I’m developing a simple android application that stores current(now) date into sqlite database.
Later it retrieve the date values from the database to an activity and plot them on the line chart with some values assigned to dates.
I have done all the coding like retrieving the saved dates from database and storing them to a ArrayList, coding for the line chart and everything. Finally I’m getting a nullpointer exception. kindly take a look at my code and provide a solution. It would be a simple task to find the exception in the program.
Here is the layout xml code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Date"
android:layout_weight="1"/>
<Button
android:id="@+id/chartButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Chart"
android:layout_weight="1"/>
</LinearLayout>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
chartButton is for showing the graph using the intent.
Here is the part of the code for line chart:
public void openChart() {
// TODO Auto-generated method stub
//Date[] dt = 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.add(date);//dt[i] = date;
//i++;
}
Double[] numbers = null; //= {20.0,20.1,20.2,20.3,20.4,20.5,20.6,20.7,20.8,20.9,21.0,21.2};
TimeSeries numbersTime = new TimeSeries("Numbers Time");
for(int i2=0;i2<dt.size();i2++){
numbers[i2]=(double) i2;
numbersTime.add(dt.get(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);
}
Here is the Cursor method in the DBAdapter class:
Cursor getAllRecords(){
String[] columns = {COL_ID,COL_DATE};
return db.query(TABLE_NAME, columns, null, null, null, null, null);
}
Here is the Manifest file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dateexample.seenu"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="4"
android:targetSdkVersion="16" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.dateexample.seenu.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="org.achartengine.GraphicalActivity" />
</application>
</manifest>
Here is the LogCat report:
01-25 18:11:39.656: D/AndroidRuntime(28121): Shutting down VM
01-25 18:11:39.656: W/dalvikvm(28121): threadid=1: thread exiting with uncaught exception (group=0x40018578)
01-25 18:11:39.992: E/AndroidRuntime(28121): FATAL EXCEPTION: main
01-25 18:11:39.992: E/AndroidRuntime(28121): java.lang.NullPointerException
01-25 18:11:39.992: E/AndroidRuntime(28121): at com.example.dateexample.seenu.MainActivity.openChart(MainActivity.java:103)
01-25 18:11:39.992: E/AndroidRuntime(28121): at com.example.dateexample.seenu.MainActivity$2.onClick(MainActivity.java:70)
01-25 18:11:39.992: E/AndroidRuntime(28121): at android.view.View.performClick(View.java:2485)
01-25 18:11:39.992: E/AndroidRuntime(28121): at android.view.View$PerformClick.run(View.java:9080)
01-25 18:11:39.992: E/AndroidRuntime(28121): at android.os.Handler.handleCallback(Handler.java:587)
01-25 18:11:39.992: E/AndroidRuntime(28121): at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 18:11:39.992: E/AndroidRuntime(28121): at android.os.Looper.loop(Looper.java:130)
01-25 18:11:39.992: E/AndroidRuntime(28121): at android.app.ActivityThread.main(ActivityThread.java:3687)
01-25 18:11:39.992: E/AndroidRuntime(28121): at java.lang.reflect.Method.invokeNative(Native Method)
01-25 18:11:39.992: E/AndroidRuntime(28121): at java.lang.reflect.Method.invoke(Method.java:507)
01-25 18:11:39.992: E/AndroidRuntime(28121): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
01-25 18:11:39.992: E/AndroidRuntime(28121): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
01-25 18:11:39.992: E/AndroidRuntime(28121): at dalvik.system.NativeStart.main(Native Method)
01-25 18:11:39.992: E/AndroidRuntime(28121): FATAL EXCEPTION: main
01-25 18:11:39.992: E/AndroidRuntime(28121): java.lang.NullPointerException
01-25 18:11:39.992: E/AndroidRuntime(28121): at com.example.dateexample.seenu.MainActivity.openChart(MainActivity.java:103)
01-25 18:11:39.992: E/AndroidRuntime(28121): at com.example.dateexample.seenu.MainActivity$2.onClick(MainActivity.java:70)
01-25 18:11:39.992: E/AndroidRuntime(28121): at android.view.View.performClick(View.java:2485)
01-25 18:11:39.992: E/AndroidRuntime(28121): at android.view.View$PerformClick.run(View.java:9080)
01-25 18:11:39.992: E/AndroidRuntime(28121): at android.os.Handler.handleCallback(Handler.java:587)
01-25 18:11:39.992: E/AndroidRuntime(28121): at android.os.Handler.dispatchMessage(Handler.java:92)
01-25 18:11:39.992: E/AndroidRuntime(28121): at android.os.Looper.loop(Looper.java:130)
01-25 18:11:39.992: E/AndroidRuntime(28121): at android.app.ActivityThread.main(ActivityThread.java:3687)
01-25 18:11:39.992: E/AndroidRuntime(28121): at java.lang.reflect.Method.invokeNative(Native Method)
01-25 18:11:39.992: E/AndroidRuntime(28121): at java.lang.reflect.Method.invoke(Method.java:507)
01-25 18:11:39.992: E/AndroidRuntime(28121): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
01-25 18:11:39.992: E/AndroidRuntime(28121): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
01-25 18:11:39.992: E/AndroidRuntime(28121): at dalvik.system.NativeStart.main(Native Method)
And finally I’m getting null pointerException showing in the logCat.
Once again I request kindly provide some solution for me.
You have a unitialized array (
Double[] numbers = null;) that you are attempting to add values to. you need to allocatenumbersarray prior to adding values to it.Double[] numbers = new Double[100];note: you might want to look into more flexible storage containers (Vectors, ArrayList, etc) that grow as you add to them.