0

I trying to draw a line chart using MpAndroidChart with values generated at runtime. The number of lines in the chart can be added or removed at the runtime also. I am using the following code to do this

private void addEntry(String label, float xValue, float yValue) {
    LineData data = mChart.getData();

    if (data != null) {

        ILineDataSet set = data.getDataSetByLabel(label, false);
        // set.addEntry(...); // can be called as well

        if (set == null) {
            set = createSet(label);
            data.addDataSet(set);
        }

        // add a new x-value first
        data.addXValue(String.valueOf(xValue));

        set.addEntry(new Entry(yValue, set.getEntryCount()));
        data.notifyDataChanged();

        // let the chart know it's data has changed
        mChart.notifyDataSetChanged();

        // move to the latest entry
        mChart.moveViewToX(data.getXValCount() - 121);
}

private LineDataSet createSet(String label) {
    LineDataSet set = new LineDataSet(null, label);
    set.setAxisDependency(YAxis.AxisDependency.LEFT);
    Random random = new Random();
    set.setColor(Color.argb(255, random.nextInt(256), random.nextInt(256), random.nextInt(256)));
    set.setCircleColor(Color.WHITE);
    set.setLineWidth(2f);
    set.setCircleRadius(1f);
    set.setFillAlpha(65);
    set.setFillColor(ColorTemplate.getHoloBlue());
    set.setHighLightColor(Color.rgb(244, 117, 117));
    set.setValueTextColor(Color.WHITE);
    set.setValueTextSize(9f);
    set.setDrawValues(false);
    return set;
}

It is almost the same code from this example at the library repository. When only one line is drawn in the chart, there is no issue. However, the problem arise when more than 2 or 3 lines are drawn at the same time with realtime data. The chart draws lines for some values and then crash with the following error:

FATAL EXCEPTION: main
   java.lang.NullPointerException: Attempt to invoke virtual method 'void com.github.mikephil.charting.buffer.LineBuffer.setPhases(float, float)' on a null object reference
   at com.github.mikephil.charting.renderer.LineChartRenderer.drawLinear(LineChartRenderer.java:309)
   at com.github.mikephil.charting.renderer.LineChartRenderer.drawDataSet(LineChartRenderer.java:129)
   at com.github.mikephil.charting.renderer.LineChartRenderer.drawData(LineChartRenderer.java:108)
   at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:251)
   at android.view.View.draw(View.java:16187)
   at android.view.View.updateDisplayListIfDirty(View.java:15184)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
   at android.view.View.updateDisplayListIfDirty(View.java:15144)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
   at android.view.View.updateDisplayListIfDirty(View.java:15144)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
   at android.view.View.updateDisplayListIfDirty(View.java:15144)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
   at android.view.View.updateDisplayListIfDirty(View.java:15144)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
   at android.view.View.updateDisplayListIfDirty(View.java:15144)
   at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3593)
   at android.view.ViewGroup.dispatchGetDisplayList(ViewGroup.java:3573)
   at android.view.View.updateDisplayListIfDirty(View.java:15144)
   at android.view.ThreadedRenderer.updateViewTreeDisplayList(ThreadedRenderer.java:281)
   at android.view.ThreadedRenderer.updateRootDisplayList(ThreadedRenderer.java:287)
   at android.view.ThreadedRenderer.draw(ThreadedRenderer.java:322)
   at android.view.ViewRootImpl.draw(ViewRootImpl.java:2615)
   at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2434)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2067)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
   at android.view.Choreographer.doCallbacks(Choreographer.java:670)
   at android.view.Choreographer.doFrame(Choreographer.java:606)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:5466)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

What is the problem here? The exception I'm getting is from within the library code Any help is greatly appreciated.

Harry
  • 1,151
  • 11
  • 27
  • The stacktrace in the logcat tells you the exact filename and line number where the error occurs. Learning to read this information is critical when learning to program Java and Android. You can find the indicated line and try to figure out what reference or return value is null. If necessary, use a debugger to help you figure this out and to find out whether you expect a possible null value or not. – Code-Apprentice Feb 19 '16 at 17:25
  • The problem is that I'm using this external library called MpAndroidChart in my application and it is in the code of this library I'm getting this exception. There is no NullPointerException from my side of code, if it was I would not have posted this question and rather have used debugger as you have suggested to find the reason. – Harry Feb 19 '16 at 18:11
  • The first place to start is to make sure you are using the library correctly. Perhaps there is an initialization step you are supposed to do first to avoid the NPE. Check the library's documentation. – Code-Apprentice Feb 19 '16 at 18:40
  • Since the library is open source, you can debug just fine. Place a breakpoint on the line which causes the NPE and find out what causes it. – Code-Apprentice Feb 19 '16 at 18:42

0 Answers0