Im trying to create an achartengine bar chart from data in my sqlite database, but when I attempt to populate the dataset for the bar chart I get the following error:
Caused by: java.lang.IllegalStateException: Couldn't read row 1, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
TestAdapter.java
public Cursor getDaysGraphDataY()
{
try
{
String sql ="SELECT data FROM v_data_main WHERE datatype = 'd' ORDER BY CAST(number AS INTEGER) DESC";
Cursor mCur = mDb.rawQuery(sql, null);
if (mCur!=null)
{
mCur.moveToNext();
}
return mCur;
}
catch (SQLException mSQLException)
{
Log.e(TAG, "getDaysGraphDataY >>"+ mSQLException.toString());
throw mSQLException;
}
}
public Cursor getDaysGraphDataX()
{
try
{
String sql ="SELECT number FROM v_data_main WHERE datatype = 'd' ORDER BY CAST(number AS INTEGER) DESC";
Cursor mCur = mDb.rawQuery(sql, null);
if (mCur!=null)
{
mCur.moveToNext();
}
return mCur;
}
catch (SQLException mSQLException)
{
Log.e(TAG, "getDaysGraphDataX >>"+ mSQLException.toString());
throw mSQLException;
}
}
BarChart.java
public class BarGraph{
public Intent getIntent(Context context)
{
// Bar 1
//int[] y = { 124, 135, 443, 456, 234, 123, 342, 134, 123, 643, 234, 274 };
//context = context.getApplicationContext();
TestAdapter mDbHelper = new TestAdapter(context);
mDbHelper.createDatabase();
mDbHelper.open();
Cursor getDataY = mDbHelper.getDaysGraphDataY();
Cursor getDataX = mDbHelper.getDaysGraphDataX();
int rowsY = getDataY.getCount();
int rowsX = getDataX.getCount();
getDataY.moveToFirst();
getDataX.moveToFirst();
CategorySeries series = new CategorySeries("Demo Bar Graph 1");
for (int i = 0; i < rowsY; i++) {
series.add(getDataX.getString(i), getDataY.getInt(i));
getDataY.moveToNext();
getDataX.moveToNext();
}
mDbHelper.close();
/*
// Bar 2
int[] y2 = { 224, 235, 243, 256, 234, 223, 242, 234, 223, 243, 234, 274 };
CategorySeries series2 = new CategorySeries("Demo Bar Graph 2");
for (int i = 0; i < y.length; i++) {
series2.add("Bar " + (i+1), y2[i]);
}
*/
XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
dataset.addSeries(series.toXYSeries());
//dataset.addSeries(series2.toXYSeries());
// This is how the "Graph" itself will look like
XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer();
mRenderer.setChartTitle("Demo Graph Title");
mRenderer.setXTitle("X VALUES");
mRenderer.setYTitle("Y VALUES");
mRenderer.setAxesColor(Color.GREEN);
mRenderer.setLabelsColor(Color.RED);
// Customize bar 1
XYSeriesRenderer renderer = new XYSeriesRenderer();
renderer.setDisplayChartValues(true);
renderer.setChartValuesSpacing((float) 0.5);
mRenderer.addSeriesRenderer(renderer);
// Customize bar 2
XYSeriesRenderer renderer2 = new XYSeriesRenderer();
renderer.setColor(Color.CYAN);
renderer.setDisplayChartValues(true);
renderer.setChartValuesSpacing((float) 0.5);
mRenderer.addSeriesRenderer(renderer2);
Intent intent = ChartFactory.getBarChartIntent(context, dataset,mRenderer, Type.DEFAULT);
return intent;
}
}
I have tried adding an _id column as suggested in another question and also uninstalling the app and reinstalling.
Here is the full logcat:
04-16 17:38:23.625 3334-3334/? E/CursorWindow: Failed to read row 1, column 1 from a CursorWindow which has 30 rows, 1 columns.
04-16 17:38:23.629 3334-3334/? D/AndroidRuntime: Shutting down VM
04-16 17:38:23.630 3334-3334/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.marnistek.serverstats, PID: 3334
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.view.View$DeclaredOnClickListener.onClick(View.java:4452)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
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)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
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)
Caused by: java.lang.IllegalStateException: Couldn't read row 1, col 1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:438)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at com.marnistek.serverstats.BarGraph.getIntent(BarGraph.java:36)
at com.marnistek.serverstats.GraphAChartEngineActivity.barGraphHandler(GraphAChartEngineActivity.java:27)
at java.lang.reflect.Method.invoke(Native Method)
at android.view.View$DeclaredOnClickListener.onClick(View.java:4447)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
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)
Any help would be much appreciated.
I got it working by changing my loop from:
for (int i = 0; i < rowsY; i++) {
series.add(getDataX.getString(i), getDataY.getInt(i));
getDataY.moveToNext();
getDataX.moveToNext();
}
To:
for (int i = 0; i < rows; i++) {
series.add(getData.getString(getData.getColumnIndex("number")), getData.getInt(getData.getColumnIndex("data")));
getData.moveToPosition(i);
}
I also changed it to use just one cursor to get all the columns I needed from the db, instead of having a separate cursor for each the X and the Y axes