Hi i'm trying to implement aChartEngine library, when i add values in code (like in this tutorial by JavaCodeGeeks ) it works perfectly but if I use a function the application crash and gives NullPointerException
on mCurrentSeries.add(x, y). When i debug to see whats the problem mCurrentSeries.add(x,y) is repeated twice (in the same cycle) and then crash.
This is the class
package com.example.achartengine;
import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;
import com.towel.math.Expression;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
private GraphicalView mChart;
private XYMultipleSeriesDataset mDataset = new XYMultipleSeriesDataset();
private XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
private XYSeries mCurrentSeries;
private XYSeriesRenderer mCurrentRenderer;
private Expression function;
private EditText textFunction;
public void initChart() {
mCurrentSeries = new XYSeries("Sample Data");
mDataset.addSeries(mCurrentSeries);
mCurrentRenderer = new XYSeriesRenderer();
mRenderer.addSeriesRenderer(mCurrentRenderer);
}
View.OnClickListener handler = new View.OnClickListener() {
@Override
public void onClick(View v) {
addGraphData();
}
};
private void addGraphData(){
String f = textFunction.getText().toString();
function = new Expression(f);
Double y;
for (int x = 0; x < 15; x++){
function.setVariable("x", x);
y = function.resolve();
mCurrentSeries.add(x, y);
}
LinearLayout layout = (LinearLayout) findViewById(R.id.chart);
if(mChart==null){
initChart();
mChart = ChartFactory.getCubeLineChartView(this, mDataset, mRenderer, 0.3f);
layout.addView(mChart);
} else {
mChart.repaint();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button draw = (Button) findViewById(R.id.buttonDraw);
draw.setOnClickListener(handler);
textFunction = (EditText) findViewById(R.id.textFunction);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
and this is the logcat
03-10 08:11:24.609: D/AndroidRuntime(2450): Shutting down VM
03-10 08:11:24.609: W/dalvikvm(2450): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
03-10 08:11:24.619: E/AndroidRuntime(2450): FATAL EXCEPTION: main
03-10 08:11:24.619: E/AndroidRuntime(2450): java.lang.NullPointerException
03-10 08:11:24.619: E/AndroidRuntime(2450): at com.example.achartengine.MainActivity.addGraphData(MainActivity.java:54)
03-10 08:11:24.619: E/AndroidRuntime(2450): at com.example.achartengine.MainActivity.access$0(MainActivity.java:46)
03-10 08:11:24.619: E/AndroidRuntime(2450): at com.example.achartengine.MainActivity$1.onClick(MainActivity.java:42)
03-10 08:11:24.619: E/AndroidRuntime(2450): at android.view.View.performClick(View.java:4204)
03-10 08:11:24.619: E/AndroidRuntime(2450): at android.view.View$PerformClick.run(View.java:17355)
03-10 08:11:24.619: E/AndroidRuntime(2450): at android.os.Handler.handleCallback(Handler.java:725)
03-10 08:11:24.619: E/AndroidRuntime(2450): at android.os.Handler.dispatchMessage(Handler.java:92)
03-10 08:11:24.619: E/AndroidRuntime(2450): at android.os.Looper.loop(Looper.java:137)
03-10 08:11:24.619: E/AndroidRuntime(2450): at android.app.ActivityThread.main(ActivityThread.java:5041)
03-10 08:11:24.619: E/AndroidRuntime(2450): at java.lang.reflect.Method.invokeNative(Native Method)
03-10 08:11:24.619: E/AndroidRuntime(2450): at java.lang.reflect.Method.invoke(Method.java:511)
03-10 08:11:24.619: E/AndroidRuntime(2450): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-10 08:11:24.619: E/AndroidRuntime(2450): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-10 08:11:24.619: E/AndroidRuntime(2450): at dalvik.system.NativeStart.main(Native Method)
You need to call initChart
in order to initialize mCurrentSeries
, before using it in onClick
.