Search code examples
javasqliteandroid-studioandroid-sqliteandroid-sql

Couldn't read row 0, col 5 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it


Log Cat

Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 5 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:465) at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51) at com.example.workhours.DataBaseHelper.ViewAllNotes(DataBaseHelper.java:90) at com.example.workhours.MainActivity.ViewAllNotes(MainActivity.java:55) at com.example.workhours.MainActivity.onCreate(MainActivity.java:37)

public  ArrayList<newNote> ViewAllNotes() {
    ArrayList<newNote> arrayList = new ArrayList<>();
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery("SELECT NOTEMEMOS FROM " + TABLE_NAME, null);

    while(cursor.moveToNext()){
        String notes = cursor.getString(5);
        newNote newNote = new newNote(notes);
        arrayList.add(newNote);
    }
    return arrayList;
}

Query

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + TABLE_NAME + "(ID INTEGER PRIMARY KEY AUTOINCREMENT, DATE TEXT, TIMESHIFTSTART INTEGER, TIMESHIFTENDS TEXT, NOTES TEXT, NOTEMEMOS TEXT)");
}

Solution

  • Your cursor has one column SELECT NOTEMEMOS but you're trying to read the sixth one with getString(5). Replace that with getString(0) to read the only column value.