Mpandroidchart: IndexOutOfBoundsException Invalid Index 0, size 0 in LegendRenderer

Created on 6 Jun 2018  路  3Comments  路  Source: PhilJay/MPAndroidChart

I am getting the following errors. These are NOT THE SAME as other outofboundsexceptions. I am plotting realtime random values in a thread. I am getting error randomly - for example, at some times, the app crashes after 10 sec but at other times after 30 sec and sometimes instantly after opening. I tried 3.0.0 to 3.0.3 versions but I am getting the same error.
`java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0

at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
at java.util.ArrayList.get(ArrayList.java:308)
at com.github.mikephil.charting.renderer.LegendRenderer.renderLegend(LegendRenderer.java:377)
at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:267)
at android.view.View.draw(View.java:17526)
at android.view.View.updateDisplayListIfDirty(View.java:16519)
at android.view.ViewGroup.recreateChildDisplayList(ViewGroup.java:3910)`

The calculatedLabelSizes arraylist is having zero length. No mistakes in my code, I copied it as it is from https://github.com/laxmimerit/Real-Time-Accelerometer-Data-Plot/blob/master/app/src/main/java/com/example/mbreath/realtimeaccelerometer/MainActivity.java.
I am not able to find out why. Please help. Thanks in advance!

Most helpful comment

I experience the same error when using a thread to inject database data into a my graphs. The problem is due to MPAndroidChart not beeing threadsafe. In other words; when another thread manipulates the graph, while the UI-thread (Main thread) is handling an event caused by user interaction, nothing is guaranteed about the internal data inside the graph, since another thread is manipulating it.

To solve the problem you should let the UI-thread update the graph for you. Include all the code that manipulates your graph-object inside a Runnable and post it to the graph. My Kotlin-code looks like this: graph.post{Push2Plot.setPlot(readings, graph, start, end)}. In Java you'll need some boilerplate-code to accomplish the same thing. You can read more here.

Suggestion to @PhilJay: Might be nice to add something about this in the readme/wiki, especially since people are forced to use separate threads when communicating with a Room database.

All 3 comments

I experience the same error when using a thread to inject database data into a my graphs. The problem is due to MPAndroidChart not beeing threadsafe. In other words; when another thread manipulates the graph, while the UI-thread (Main thread) is handling an event caused by user interaction, nothing is guaranteed about the internal data inside the graph, since another thread is manipulating it.

To solve the problem you should let the UI-thread update the graph for you. Include all the code that manipulates your graph-object inside a Runnable and post it to the graph. My Kotlin-code looks like this: graph.post{Push2Plot.setPlot(readings, graph, start, end)}. In Java you'll need some boilerplate-code to accomplish the same thing. You can read more here.

Suggestion to @PhilJay: Might be nice to add something about this in the readme/wiki, especially since people are forced to use separate threads when communicating with a Room database.

@athathdev please let me know if this helped you solve your problem.

@exoji2e You solved my problem perfectly.Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

thanhcly920 picture thanhcly920  路  3Comments

andreyfel picture andreyfel  路  3Comments

AndroidJiang picture AndroidJiang  路  3Comments

mrZizik picture mrZizik  路  3Comments

manucheri picture manucheri  路  3Comments