Reputation: 85
I have a little problem with MPAndroidChart. In the code above i get an error which are make me mad for 3 days. Im getting an error when i try to add a entry to a barEntry array and i don no why.
private void retrive(){
NoSQL.with(this).using(Float.class)
.bucketId("MyMarket")
.entityId("Chart")
.retrieve(new RetrievalCallback<Float>() {
@Override
public void retrievedResults(List<NoSQLEntity<Float>> noSQLEntities) {
for(int i = 0; i < noSQLEntities.size(); i++) {
float item = noSQLEntities.get(i).getData();
BarEntry entry = new BarEntry(i, item);
barEntries.add(entry);
}
}
});
BarDataSet barDataSet = new BarDataSet(barEntries, "Compras");
BarData theData = new BarData(barDataSet);
theData.setBarWidth(0.9f);
barChart.setData(theData);
barChart.setTouchEnabled(true);
barChart.setDragEnabled(true);
barChart.setScaleEnabled(true);
barChart.setDescription("");
barChart.animateY(2700);
barChart.setData(theData);
barChart.setFitBars(true);
XAxis xAxis = barChart.getXAxis();
xAxis.setDrawGridLines(false);
YAxis right = barChart.getAxisRight();
right.setDrawGridLines(false);
YAxis left = barChart.getAxisLeft();
left.setDrawGridLines(false);
barChart.invalidate();
}
In this line:
BarEntry entry = new BarEntry(i, item);
I have sure the value is added, but the program keep give me this error:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.stark.mymarket, PID: 6404
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
at com.github.mikephil.charting.buffer.BarBuffer.addBar(BarBuffer.java:37)
at com.github.mikephil.charting.buffer.BarBuffer.feed(BarBuffer.java:80)
at com.github.mikephil.charting.renderer.BarChartRenderer.drawDataSet(BarChartRenderer.java:103)
at com.github.mikephil.charting.renderer.BarChartRenderer.drawData(BarChartRenderer.java:78)
at com.github.mikephil.charting.charts.BarLineChartBase.onDraw(BarLineChartBase.java:243)
at android.view.View.draw(View.java:16178)
at android.view.View.updateDisplayListIfDirty(View.java:15174)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.updateDisplayListIfDirty(View.java:15169)
at android.view.View.draw(View.java:15948)
at android.view.ViewGroup.drawChild(ViewGroup.java:3609)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:3399)
at android.view.View.draw(View.java:16181)
at com.android.internal.policy.PhoneWindow$DecorView.draw(PhoneWindow.java:2690)
at android.view.View.updateDisplayListIfDirty(View.java:15174)
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:5417)
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)
Upvotes: 2
Views: 494
Reputation: 13199
You should look at your console log. Focusing on the line in which it says to you what kind of error you are getting:
java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
which means that you are trying to access to a wrong position of your list, noSQLEntities
.
You are getting the error on this line:
BarEntry entry = new BarEntry(i, item);
but I am almost secure that your error it is really on this other line (which is used on the line above):
float item = noSQLEntities.get(i).getData();
It seems that you are not initializing your noSQLEntities
list with values so you are trying to access to a position of that list that does not exist.
Make sure that your noSQLEntities
list have values. You can use debug mode (if you are using Android Studio) or logs
to see the values of your noSQLEntities
list in your console.
Upvotes: 1