here is the part of the code of my application that plots the values for a particular date on the line chart. The dates are retrieved from the database in Android.
Here is the code for the line chart that is in MainActivity.java:
public void openChart() {
// TODO Auto-generated method stub
Date[] dt = new Date[100];//new Date[c.getCount()];
int i=0;
c=dbAdapter.getAllRecords();
c.moveToFirst();
//List<Date> dt = new ArrayList<Date>();
while(c.moveToNext()){
String stringDate = c.getString(1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date date = null;
try {
date = sdf.parse(stringDate);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
dt[i] = date;//dt.add(date);//
i++;
}
//double[] numbers = {20.0,20.1,20.2,20.3,20.4,20.5,20.6,20.7,20.8,20.9,21.0,21.2};
Double[] numbers = new Double[100];
TimeSeries numbersTime = new TimeSeries("Numbers Time");
for(int i2=0;i2<dt.length;i2++){
numbers[i2]=(double) i2;
numbersTime.add(dt[i2],numbers[i2]);
}
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(numbersTime);
XYSeriesRenderer numbersRenderer = new XYSeriesRenderer();
numbersRenderer.setColor(Color.BLACK);
numbersRenderer.setPointStyle(PointStyle.CIRCLE);
numbersRenderer.setFillPoints(true);
numbersRenderer.setLineWidth(2);
numbersRenderer.setDisplayChartValues(true);
XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
multiRenderer.setChartTitle("Visits vs Views Chart");
multiRenderer.setXTitle("Date Time");
multiRenderer.setYTitle("Double Numbers");
multiRenderer.setZoomButtonsVisible(true);
multiRenderer.addSeriesRenderer(numbersRenderer);
// Creating an intent to plot line chart using dataset and multipleRenderer
Intent intent = ChartFactory.getLineChartIntent(getBaseContext(), dataset, multiRenderer);
// Start Activity
startActivity(intent);
}
And here goes LogCat file lines:
01-25 19:14:30.531: D/AndroidRuntime(31866): Shutting down VM
01-25 19:14:30.531: W/dalvikvm(31866): threadid=1: thread exiting with uncaught exception (group=0x40018578)
01-25 19:14:30.546: E/AndroidRuntime(31866): FATAL EXCEPTION: main
01-25 19:14:30.546: E/AndroidRuntime(31866): java.lang.NullPointerException
01-25 19:14:30.546: E/AndroidRuntime(31866): at org.achartengine.model.TimeSeries.add(TimeSeries.java:41)
01-25 19:14:30.546: E/AndroidRuntime(31866): at com.example.dateexample.seenu.MainActivity.openChart(MainActivity.java:105)
01-25 19:14:30.546: E/AndroidRuntime(31866): at com.example.dateexample.seenu.MainActivity$2.onClick(MainActivity.java:70)
01-25 19:14:30.546: E/AndroidRuntime(31866): at android.view.View.performClick(View.java:2485)
01-25 19:14:30.546: E/AndroidRuntime(31866): at android.view.View$PerformClick.run(View.java:9080)
01-25 19:14:30.546: E/AndroidRuntime(31866): at android.os.Handler.handleCallback(Handler.java:587)
01-25 19:14:30.546: E/AndroidRuntime(31866): at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 19:14:30.546: E/AndroidRuntime(31866): at android.os.Looper.loop(Looper.java:130)
01-25 19:14:30.546: E/AndroidRuntime(31866): at android.app.ActivityThread.main(ActivityThread.java:3687)
01-25 19:14:30.546: E/AndroidRuntime(31866): at java.lang.reflect.Method.invokeNative(Native Method)
01-25 19:14:30.546: E/AndroidRuntime(31866): at java.lang.reflect.Method.invoke(Method.java:507)
01-25 19:14:30.546: E/AndroidRuntime(31866): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
01-25 19:14:30.546: E/AndroidRuntime(31866): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
01-25 19:14:30.546: E/AndroidRuntime(31866): at dalvik.system.NativeStart.main(Native Method)
Here I'm getting nullPointer exception at line:105, it is 'numbersTime.add(dt[i2],numbers[i2]);' statement. I'm not clear how to retrieve the dates from the database and plot them on the chat. Kindly provide solution for my exception.
P.S: If my question is not clear ask me some edits. Thanku.
It looks like your
dt[i2]
is null at some point, which is probably caused by yourdate = sdf.parse(stringDate);
. If the parse fails, thedate
doesn't get set so it will stay null.Try setting it to a default value in your catch, or check for null specifically before you do
numbersTime.add(dt[i2],numbers[i2])
In my project, I have sth. like this:
valueY
isString
array.and I get this chart like this:
this is my
parseDayAndMonth()
method.