Blog Archive

Android SQLITE error android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

10-08 01:42:57.908: ERROR/AndroidRuntime(421): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:421)
10-08 01:42:57.908: ERROR/AndroidRuntime(421): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:117)
10-08 01:42:57.908: ERROR/AndroidRuntime(421): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:31)

The issue here is accessing the cursor when no records were returned. First check if there is data and then access the columns using the cursor. Here is sample code...
Cursor mCursor = mDb.query(SQLiteDatabase db, 
     String[] projectionIn, 
     String selection, 
     String[] selectionArgs, 
     String groupBy, 
     String having, 
     String sortOrder, 
     String limit);
if (mCursor != null) {
 if (mCursor.moveToFirst()) {
  String myData = mCursor.getString(mCursor.getColumnIndexOrThrow(KEY_DATA));
 }
}
if (mCursor != null && !mCursor.isClosed()) {
 mCursor.close();
}

1 comment :