Note that there are some explanatory texts on larger screens.

plurals
  1. POhow to plot the dates retrieved from database on the LineChart in Android
    text
    copied!<p>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.</p> <p>Here is the layout xml code: </p> <pre><code>&lt;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" &gt; &lt;LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" &gt; &lt;Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Add Date" android:layout_weight="1"/&gt; &lt;Button android:id="@+id/chartButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Chart" android:layout_weight="1"/&gt; &lt;/LinearLayout&gt; &lt;ListView android:id="@+id/listView1" android:layout_width="match_parent" android:layout_height="wrap_content" &gt; &lt;/ListView&gt; &lt;/LinearLayout&gt; </code></pre> <p>chartButton is for showing the graph using the intent.</p> <p>Here is the part of the code for line chart:</p> <pre><code>public void openChart() { // TODO Auto-generated method stub //Date[] dt = new Date[c.getCount()]; //int i=0; c=dbAdapter.getAllRecords(); c.moveToFirst(); List&lt;Date&gt; dt = new ArrayList&lt;Date&gt;(); 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&lt;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); } </code></pre> <p>Here is the Cursor method in the DBAdapter class:</p> <pre><code>Cursor getAllRecords(){ String[] columns = {COL_ID,COL_DATE}; return db.query(TABLE_NAME, columns, null, null, null, null, null); } </code></pre> <p>Here is the Manifest file:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.dateexample.seenu" android:versionCode="1" android:versionName="1.0" &gt; &lt;uses-sdk android:minSdkVersion="4" android:targetSdkVersion="16" /&gt; &lt;application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" &gt; &lt;activity android:name="com.example.dateexample.seenu.MainActivity" android:label="@string/app_name" &gt; &lt;intent-filter&gt; &lt;action android:name="android.intent.action.MAIN" /&gt; &lt;category android:name="android.intent.category.LAUNCHER" /&gt; &lt;/intent-filter&gt; &lt;/activity&gt; &lt;activity android:name="org.achartengine.GraphicalActivity" /&gt; &lt;/application&gt; &lt;/manifest&gt; </code></pre> <p>Here is the LogCat report:</p> <pre><code>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) </code></pre> <p>And finally I'm getting null pointerException showing in the logCat. Once again I request kindly provide some solution for me.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload