Search code examples
javaandroidandroid-graphview

Couldn't read row 0, col -1 from CursorWindow


I am having trouble trying get a graph working in my app but I don't know where i'm going wrong with this code. The code builds but crashes selecting the activity. I think I may have structured it incorrectly as I may be trying create a graph before I obtain data from the database. If anyone can point out my mistake, I would be grateful.

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_graph);
    myDB = new DatabaseCode(this);
    SQLiteDatabase db = myDB.getWritableDatabase();
    Cursor cursor=db.rawQuery("select * from "+TABLE_NAME,null);
        if(cursor.getCount()==0){
            return;
        }
        else {
            ArrayList<Integer> ListArray = new ArrayList<Integer>();
            //
            while (cursor.moveToNext()) {
                ListArray.add(cursor.getInt(cursor.getColumnIndex("COL_2")));
            }
            GraphView graph = (GraphView) findViewById(R.id.graph);
            LineGraphSeries<DataPoint> series = new LineGraphSeries<DataPoint>(new DataPoint[] {

                    new DataPoint(0,ListArray.get(0)),
                    new DataPoint(1,ListArray.get(1)),
                    new DataPoint(2,ListArray.get(2))
            });
            graph.addSeries(series);
        }


}

}

Here is the error log.

04-30 16:34:18.912 20125-20125/com.example.fitmess.project E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.example.fitmess.project, PID: 20125
                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fitmess.project/com.example.fitmess.project.GraphActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3319)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415)
                                                                             at android.app.ActivityThread.access$1100(ActivityThread.java:229)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:148)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:7331)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                                                                          Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                                                                             at android.database.CursorWindow.nativeGetLong(Native Method)
                                                                             at android.database.CursorWindow.getLong(CursorWindow.java:524)
                                                                             at android.database.CursorWindow.getInt(CursorWindow.java:591)
                                                                             at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:69)
                                                                             at com.example.fitmess.project.GraphActivity.onCreate(GraphActivity.java:36)
                                                                             at android.app.Activity.performCreate(Activity.java:6904)
                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1136)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3266)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3415) 
                                                                             at android.app.ActivityThread.access$1100(ActivityThread.java:229) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1821) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:148) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:7331) 
                                                                             at java.lang.reflect.Method.invoke(Native Method) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 

Solution

  • while accessing data from cursor you have to check data in cursor and move cursor to first location and the looping will be done like below example

    if(cursor.moveToFirst()) //this will move cursor to first position
    {
       do{
    //your code to fetch data from cursor
      ListArray.add(cursor.getInt(cursor.getColumnIndex("COL_2")));
        }while(cursor.moveToNext());
    }
    

    also check the "COL_2" column name is right one as per schema.