3

I am writing a code in which the graph must be continiously updated by repeatedly calling the aSyncTask. but to ensure thet the graph doesnot get too compressed, I plan to remove/ delete the oldest entry beyond a point. Presently my code is as follows:

int counter = 0;
private class UpdateLineGraphAsync extends AsyncTask<Void, Void, Void> {

    @Override
    protected Void doInBackground(Void... params) {
        // add the next series value
        series.add(counter, Math.sin((double) counter / 100));
        counter += 20;
        if (counter > 500)
            series.remove(0);
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);
        ((GraphicalView) lineGraph).repaint(); // repaints the graph!
    }

}

But this code keeps giving me a null point exception when i call remove(0); Can anyone explain how the function XYSeries.remove(index) works? I read somewhere that it is a LIFO structure. is this true? How to correct this error? even a single call of the remove function gives an error.

The error codes are:

07-03 17:27:36.080: E/AndroidRuntime(1229): FATAL EXCEPTION: AsyncTask #2
07-03 17:27:36.080: E/AndroidRuntime(1229): java.lang.RuntimeException: An error occured while executing doInBackground()
07-03 17:27:36.080: E/AndroidRuntime(1229):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
07-03 17:27:36.080: E/AndroidRuntime(1229):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
07-03 17:27:36.080: E/AndroidRuntime(1229):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
07-03 17:27:36.080: E/AndroidRuntime(1229):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
07-03 17:27:36.080: E/AndroidRuntime(1229):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
07-03 17:27:36.080: E/AndroidRuntime(1229):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
07-03 17:27:36.080: E/AndroidRuntime(1229):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
07-03 17:27:36.080: E/AndroidRuntime(1229):     at java.lang.Thread.run(Thread.java:1027)
07-03 17:27:36.080: E/AndroidRuntime(1229): Caused by: java.lang.NullPointerException
07-03 17:27:36.080: E/AndroidRuntime(1229):     at org.achartengine.model.XYSeries.getY(XYSeries.java:169)
07-03 17:27:36.080: E/AndroidRuntime(1229):     at org.achartengine.model.XYSeries.initRange(XYSeries.java:83)
07-03 17:27:36.080: E/AndroidRuntime(1229):     at org.achartengine.model.XYSeries.remove(XYSeries.java:140)
07-03 17:27:36.080: E/AndroidRuntime(1229):     at com.clearbridgevitalsigns.cardioleaf.LiveView$UpdateLineGraphAsync.doInBackground(LiveView.java:121)
07-03 17:27:36.080: E/AndroidRuntime(1229):     at com.clearbridgevitalsigns.cardioleaf.LiveView$UpdateLineGraphAsync.doInBackground(LiveView.java:1)
07-03 17:27:36.080: E/AndroidRuntime(1229):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
07-03 17:27:36.080: E/AndroidRuntime(1229):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
07-03 17:27:36.080: E/AndroidRuntime(1229):     ... 4 more
Rahul
  • 131
  • 1
  • 7

3 Answers3

2

I used to have the same issue. I didn't know why but I'm suppose it's due to concurrent access to series data from achartengine. i managed to avoid this issue using another method to move the display, using renderer.setXAxisMin et renderer.setXAxisMax without working on the series themselves.

Got
  • 46
  • 5
0

First remove 0th element if collection exceeds limit and then try to add new element.

CSharp
  • 1,573
  • 2
  • 14
  • 25
0

I don't think that this NullPointerException is because of series.remove(0) statement. It works fine in my prototype. My view is you need to add your XYSeries in XYMultipleSeriesDataset. Refer this link for example.

Community
  • 1
  • 1
CSharp
  • 1,573
  • 2
  • 14
  • 25