Dark Shadow
Dark Shadow

Reputation: 33

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)");
}

Upvotes: 2

Views: 4866

Answers (2)

Kishan Thakkar
Kishan Thakkar

Reputation: 459

Try moving cursor to first before getting any data and you are selecting only on column from database but you are asking for 5th .

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

cursor.moveToFirst();

newNote newnote = new newNote(cursor.getString(0));
arrayList.add(newnote);

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

Upvotes: 0

laalto
laalto

Reputation: 152817

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.

Upvotes: 3

Related Questions