0

My app crashes when I run it on my device. According to logCat, the Cursor can't read the data from CursorWindow, which has 2 rows and 2 columns, in the following method:

public ArrayList<Notes> getData() {
            SQLiteDatabase db = this.getReadableDatabase();
        ArrayList<Notes> listNotes = new ArrayList<Notes>();
            result = db.rawQuery("SELECT * from " + NOTES_TABLE_NAME, new String[] {});
            if (result != null && result.getCount() > 0) {
                if (result.moveToFirst()) {
                    do {
                    Notes note = new Notes();
                    note.id = result.getInt(result.getColumnIndex(NOTES_COLUMN_ID));
                    note.text = result.getString(result.getColumnIndex(NOTES_COLUMN_NAME));
                    listNotes.add(note);
                } while (result.moveToNext());
                }
            }
            result.close();
            return listNotes;
    }

logCat

15:50:35.673    5531    agenda.com  ERROR   CursorWindow    Failed to read row 0, column -1 from a CursorWindow which has 2 rows, 2 columns.
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime  FATAL EXCEPTION: main
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime  java.lang.RuntimeException: Unable to start activity ComponentInfo{agenda.com/agenda.com.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2394)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2446)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.app.ActivityThread.access$600(ActivityThread.java:165)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.os.Handler.dispatchMessage(Handler.java:107)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.os.Looper.loop(Looper.java:194)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.app.ActivityThread.main(ActivityThread.java:5434)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at java.lang.reflect.Method.invokeNative(Native Method)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at java.lang.reflect.Method.invoke(Method.java:525)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:834)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at dalvik.system.NativeStart.main(Native Method)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime  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.
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.database.CursorWindow.nativeGetLong(Native Method)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.database.CursorWindow.getLong(CursorWindow.java:507)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.database.CursorWindow.getInt(CursorWindow.java:574)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:69)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at agenda.com.db.NotesDbHelper.getData(NotesDbHelper.java:52)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at agenda.com.MainActivity.onCreate(MainActivity.java:59)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.app.Activity.performCreate(Activity.java:5122)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1146)
15:50:35.678    5531    agenda.com  ERROR   AndroidRuntime      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2358)
Al Lelopath
  • 6,448
  • 13
  • 82
  • 139
John
  • 11
  • 4

1 Answers1

0

Check your column name for NOTES_COLUMN_ID as result.getColumnIndex(NOTES_COLUMN_ID) is returning -1, meaning that the column name could not be found in your cursor. Are you using the same constant in your CREATE TABLE statement?

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64