-1

I am getting an error Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. I have tried everything, but nothing seems to work

Variables:

private static final String COLUMN_SERVICE_DETAILS = " service_details";
private static final String COLUMN_SERVICE_TIME = " service_time";
private static final String COLUMN_SERVICE_COST = " service_cost";
private static final String TABLE_DETAILS = "details";

Table creation

    query = "CREATE TABLE " +TABLE_DETAILS+"("
            +COLUMN_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "
            +COLUMN_SERVICE_TIME+ " TEXT, "
            +COLUMN_SERVICE_DETAILS+ " TEXT, "
            +COLUMN_SERVICE_COST+ " TEXT " +
            ");";
    sqLiteDatabase.execSQL(query);

After debugging my app it stopped on this method getUserDetails

public UserDetails getUserDetails(){

    UserDetails ud = new UserDetails();
    String selectQuery = "select * from  " + TABLE_DETAILS + " where 1;";

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);
    // Move to first row
    cursor.moveToFirst();
    while (!cursor.isAfterLast()) {

        ud.set_service_details(cursor.getString(cursor.getColumnIndex(COLUMN_SERVICE_DETAILS)));
        ud.set_service_time(cursor.getString(cursor.getColumnIndex(COLUMN_SERVICE_TIME)));
        ud.set_service_cost(cursor.getString(cursor.getColumnIndex(COLUMN_SERVICE_COST)));


        cursor.moveToNext();
    }
    cursor.close();
    db.close();

    return ud;
}

Error log:

03-17 17:27:03.553 14627-14627/com.automobilecare.automobilecare E/AndroidRuntime: FATAL EXCEPTION: main                                                                               
              Process: com.automobilecare.automobilecare, PID: 14627
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.automobilecare.automobilecare/com.automobilecare.automobilecare.userArea.UserAreaActivity}: 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:2584)
               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666)
               at android.app.ActivityThread.-wrap11(ActivityThread.java)
               at android.app.Activitat android.os.Handler.dispatchMessage(Handler.java:111)
               at android.os.Looper.loop(Looper.java:207)
               at android.app.ActivityThread.main(ActivityThread.java:5769)
               at java.lang.reflect.Method.invoke(Native Method)
               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
               at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)
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.nativeGetString(Native Method)
               at android.database.CursorWindow.getString(CursorWindow.java:438)
               at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:66)
               at com.automobilecare.automobilecare.internalDBConnectivity.DBHandler.getUserDetails(DBHandler.java:240)
               at com.automobilecare.automobilecare.userArea.UserAreaActivity.onCreate(UserAreaActivity.java:85)
               at android.app.Activity.performCreate(Activity.java:6583)
               at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1114)
               at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2531)
               at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2666) 
               at android.app.ActivityThread.-wrap11(ActivityThread.java) 
               at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1493) 
               at android.os.Handler.dispatchMessage(Handler.java:111) 
               at android.os.Looper.loop(Looper.java:207) 
               at android.app.ActivityThread.main(ActivityThread.java:5769) 
               at java.lang.reflect.Method.invoke(Native Method) 
               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789) 
               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679) 
               at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:102)

 

Thank you for your help

Jitesh Prajapati
  • 2,533
  • 4
  • 29
  • 51
Devansh Jani
  • 57
  • 1
  • 10
  • 1
    use `getColumnIndexOrThrow` instead of `getColumnIndex` – pskink Mar 17 '17 at 12:09
  • 1
    Please, remove `+ " where 1;";`. This is not only **useless**, but also potentially **harmful** (it will make your db prone to `SQL Injection`) – Phantômaxx Mar 17 '17 at 12:09
  • The error itself just means **"no such column"** ... that's why it better to not use `select *` because you don't know what columns you will gets – Selvin Mar 17 '17 at 12:11
  • java.lang.IllegalArgumentException: column ' service_details' does not exist but I have properly created table and it does have a column named service_details – Devansh Jani Mar 17 '17 at 12:12

1 Answers1

5

Remove the leading spaces in your column names:

private static final String COLUMN_SERVICE_DETAILS = " service_details";
private static final String COLUMN_SERVICE_TIME = " service_time";
private static final String COLUMN_SERVICE_COST = " service_cost";

Change to

private static final String COLUMN_SERVICE_DETAILS = "service_details";
private static final String COLUMN_SERVICE_TIME = "service_time";
private static final String COLUMN_SERVICE_COST = "service_cost";

SQL does not care about such whitespace but the Map-like internal representation a Cursor wants to see the columns exactly as in SQL (where spaces are ignored).

laalto
  • 150,114
  • 66
  • 286
  • 303