AChartEngine crashes after adding points and panning

I'm building an Android app with AChartEngine and am getting weird errors.

After I add new points to my already established chart and touch/pan the graph, I get a NullPointerException in LineChart(XYChart).getSeriesAndPointForScreenCoordinate(Point), where GraphicalView's variables oldX and oldY are discovered to be null. These variables are set in GraphicalView.onTouchEvent(), but that is apparently not being called.

Is there a way to manually trigger onTouchEvent() when initializing my graph, or is there a workaround to this error?

Here is my chart set up 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();
        }
    }
});

Here is how I add new points:

protected class NewPoints extends AsyncTask<Double, Void, Void> {
    @Override
    protected Void doInBackground(Double... values) {
        mCurrentSeries.add(values[0], values[1]); // x, y

        if (mChartView != null) {
            mChartView.repaint();
        }
        return null;
    }
}

My testing code:

for(double i = 0; i < 10; i++) {
    new NewPoints().execute(i, Math.log(i));
}

Here is the LogCat output:

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)

Update 1:

I've recompiled with the AChartEngine source code and debugging shows that the error takes place in this method:

public SeriesSelection getSeriesAndPointForScreenCoordinate(final Point screenPoint) {
    if (clickableAreas != null)
      for (int seriesIndex = clickableAreas.size() - 1; seriesIndex >= 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 && rectangle.contains(screenPoint.getX(), screenPoint.getY())) {
              return new SeriesSelection(seriesIndex, pointIndex, area.getX(), area.getY());
            }
            pointIndex++;
          }
        }
      }
    return super.getSeriesAndPointForScreenCoordinate(screenPoint);
  }

The exception is thrown at the rectangle = area.getRect(); line, where area is null since clickableAreas.get(0) somehow returns a collection of null clickableAreas.

I'm going to try to patch this with a check to see if the clickableArea is null, but this begs the question: am I adding points correctly?

Answers


As per the stacktrace, the exception occurs in the second line in the following code of XYChart.java (if my source code has matching line numbers):

for (ClickableArea area : clickableAreas.get(seriesIndex)) {
    rectangle = area.getRect();  // <--- Exception here!
    if (rectangle != null && rectangle.contains(screenPoint.getX(), screenPoint.getY())) {
        return new SeriesSelection(seriesIndex, pointIndex, area.getX(), area.getY());
    }
    pointIndex++;
}

I can't see how that can happen.

Can you compile your code with AChartEngine's source and debug?

Further oldX & oldY are both declared as primitive floats in GraphicalView.java. How can they be null if they are primitives?


Null pointer Exception might be Caused by your AchartEngine jar files sometimes goes to out of usage while no longer used it. So you should check the jar files on your build path by right click on your project . From that choose the options

    Order and Export->choose your jar files->click up and down button. 

It could be used to turning on your jar files used in your project.


Need Your Help

How can I interpolate georeferenced data in python?

python statistics spatial-interpolation

I have many lines of georeferenced hydrological data with weekly resolution:

Display content of files in a particular directory using Flask

python flask directory-structure static-files

I am implementing an app using Flask and I am trying to display the content of text files I put in log directory. So i have done this: