Note that there are some explanatory texts on larger screens.

plurals
  1. POAChartEngine crashes after adding points and panning
    text
    copied!<p>I'm building an Android app with <a href="http://code.google.com/p/achartengine" rel="nofollow">AChartEngine</a> and am getting weird errors.</p> <p><strong>After I add new points to my already established chart and touch/pan the graph, I get a <code>NullPointerException</code></strong> in <code>LineChart(XYChart).getSeriesAndPointForScreenCoordinate(Point)</code>, where <code>GraphicalView</code>'s variables <code>oldX</code> and <code>oldY</code> are discovered to be null. These variables are set in <code>GraphicalView.onTouchEvent()</code>, but that is apparently not being called.</p> <p>Is there a way to manually trigger <code>onTouchEvent()</code> when initializing my graph, or is there a workaround to this error?</p> <p>Here is my chart set up code:</p> <pre><code>mChartView = ChartFactory.getLineChartView(this, mDataset, mRenderer); mChartView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { SeriesSelection seriesSelection = mChartView.getCurrentSeriesAndPoint(); double[] xy = mChartView.toRealPoint(0); if (seriesSelection == null) { Toast.makeText(viewer.this, "No chart element was clicked", Toast.LENGTH_SHORT).show(); } else { Toast.makeText( viewer.this, "Chart element clicked", Toast.LENGTH_SHORT) .show(); } } }); </code></pre> <p>Here is how I add new points:</p> <pre><code>protected class NewPoints extends AsyncTask&lt;Double, Void, Void&gt; { @Override protected Void doInBackground(Double... values) { mCurrentSeries.add(values[0], values[1]); // x, y if (mChartView != null) { mChartView.repaint(); } return null; } } </code></pre> <p>My testing code:</p> <pre><code>for(double i = 0; i &lt; 10; i++) { new NewPoints().execute(i, Math.log(i)); } </code></pre> <p>Here is the LogCat output:</p> <pre><code>07-20 18:46:12.570: E/AndroidRuntime(13800): FATAL EXCEPTION: main 07-20 18:46:12.570: E/AndroidRuntime(13800): java.lang.NullPointerException 07-20 18:46:12.570: E/AndroidRuntime(13800): at org.achartengine.chart.XYChart.getSeriesAndPointForScreenCoordinate(XYChart.java:840) 07-20 18:46:12.570: E/AndroidRuntime(13800): at org.achartengine.GraphicalView.getCurrentSeriesAndPoint(GraphicalView.java:135) 07-20 18:46:12.570: E/AndroidRuntime(13800): at com.maximz.viewer$1.onClick(viewer.java:185) 07-20 18:46:12.570: E/AndroidRuntime(13800): at android.view.View.performClick(View.java:3511) 07-20 18:46:12.570: E/AndroidRuntime(13800): at android.view.View$PerformClick.run(View.java:14110) 07-20 18:46:12.570: E/AndroidRuntime(13800): at android.os.Handler.handleCallback(Handler.java:605) 07-20 18:46:12.570: E/AndroidRuntime(13800): at android.os.Handler.dispatchMessage(Handler.java:92) 07-20 18:46:12.570: E/AndroidRuntime(13800): at android.os.Looper.loop(Looper.java:137) 07-20 18:46:12.570: E/AndroidRuntime(13800): at android.app.ActivityThread.main(ActivityThread.java:4424) 07-20 18:46:12.570: E/AndroidRuntime(13800): at java.lang.reflect.Method.invokeNative(Native Method) 07-20 18:46:12.570: E/AndroidRuntime(13800): at java.lang.reflect.Method.invoke(Method.java:511) 07-20 18:46:12.570: E/AndroidRuntime(13800): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 07-20 18:46:12.570: E/AndroidRuntime(13800): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 07-20 18:46:12.570: E/AndroidRuntime(13800): at dalvik.system.NativeStart.main(Native Method) </code></pre> <p><strong>Update 1:</strong></p> <p>I've recompiled with the AChartEngine source code and debugging shows that the error takes place in this method:</p> <pre><code>public SeriesSelection getSeriesAndPointForScreenCoordinate(final Point screenPoint) { if (clickableAreas != null) for (int seriesIndex = clickableAreas.size() - 1; seriesIndex &gt;= 0; seriesIndex--) { // series 0 is drawn first. Then series 1 is drawn on top, and series 2 // on top of that. // we want to know what the user clicked on, so traverse them in the // order they appear on the screen. int pointIndex = 0; if (clickableAreas.get(seriesIndex) != null) { RectF rectangle; for (ClickableArea area : clickableAreas.get(seriesIndex)) { rectangle = area.getRect(); // EXCEPTION HERE: area is null if (rectangle != null &amp;&amp; rectangle.contains(screenPoint.getX(), screenPoint.getY())) { return new SeriesSelection(seriesIndex, pointIndex, area.getX(), area.getY()); } pointIndex++; } } } return super.getSeriesAndPointForScreenCoordinate(screenPoint); } </code></pre> <p>The exception is thrown at the <code>rectangle = area.getRect();</code> line, where <code>area</code> is null since <strong><code>clickableAreas.get(0)</code> somehow returns a collection of null clickableAreas</strong>.</p> <p>I'm going to try to patch this with a check to see if the clickableArea is null, but this begs the question: <strong>am I adding points correctly?</strong></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