Android.database.CursorIndexOutOfBoundsException

The data access code in my Android application throws an exception.

Here is the code that throws the exception:

String getPost = "SELECT * FROM " + TABLE_POST + ";"; Cursor result = getReadableDatabase().rawQuery(getPost, null); List posts = new ArrayList();{ Log.d("count", result.getCount() + " post rows"); while (result != null && result.moveToNext()); Post post = new Post(); post.setPostId(result.getInt(0)); posts.add(post); .... } 

The exception is:

 android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2 exception 

The log method gives the result 2 post rows .

What is wrong here?

Edit: I added a full stack trace so that anyone who wants to view it can do this.

 11-14 09:57:50.538: W/dalvikvm(4710): threadid=1: thread exiting with uncaught exception (group=0x40a71930) 11-14 09:57:50.608: E/AndroidRuntime(4710): FATAL EXCEPTION: main 11-14 09:57:50.608: E/AndroidRuntime(4710): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.innolabmm.software.collaboration/com.innolabmm.software.collaboration.PostCommentActivity}: android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2 11-14 09:57:50.608: E/AndroidRuntime(4710): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 11-14 09:57:50.608: E/AndroidRuntime(4710): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 11-14 09:57:50.608: E/AndroidRuntime(4710): at android.app.ActivityThread.access$600(ActivityThread.java:141) 11-14 09:57:50.608: E/AndroidRuntime(4710): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 11-14 09:57:50.608: E/AndroidRuntime(4710): at android.os.Handler.dispatchMessage(Handler.java:99) 11-14 09:57:50.608: E/AndroidRuntime(4710): at android.os.Looper.loop(Looper.java:137) 11-14 09:57:50.608: E/AndroidRuntime(4710): at android.app.ActivityThread.main(ActivityThread.java:5041) 11-14 09:57:50.608: E/AndroidRuntime(4710): at java.lang.reflect.Method.invokeNative(Native Method) 11-14 09:57:50.608: E/AndroidRuntime(4710): at java.lang.reflect.Method.invoke(Method.java:511) 11-14 09:57:50.608: E/AndroidRuntime(4710): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 11-14 09:57:50.608: E/AndroidRuntime(4710): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 11-14 09:57:50.608: E/AndroidRuntime(4710): at dalvik.system.NativeStart.main(Native Method) 11-14 09:57:50.608: E/AndroidRuntime(4710): Caused by: android.database.CursorIndexOutOfBoundsException: Index 2 requested, with a size of 2 11-14 09:57:50.608: E/AndroidRuntime(4710): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:424) 11-14 09:57:50.608: E/AndroidRuntime(4710): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136) 11-14 09:57:50.608: E/AndroidRuntime(4710): at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68) 11-14 09:57:50.608: E/AndroidRuntime(4710): at com.innolabmm.software.collaboration.data.CollaborationDbHandler.fetchAllPost(CollaborationDbHandler.java:362) 11-14 09:57:50.608: E/AndroidRuntime(4710): at com.innolabmm.software.collaboration.PostCommentActivity.onCreate(PostCommentActivity.java:48) 11-14 09:57:50.608: E/AndroidRuntime(4710): at android.app.Activity.performCreate(Activity.java:5104) 11-14 09:57:50.608: E/AndroidRuntime(4710): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 11-14 09:57:50.608: E/AndroidRuntime(4710): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 11-14 09:57:50.608: E/AndroidRuntime(4710): ... 11 more 
+8
source share
4 answers

You are trying to get an element at index 2, but this index does not actually exist (the size of the cursor is 2, so the indices are 0.1).

Change your loop:

 if (result != null && result.moveToFirst()){ do { Post post = new Post(); post.setPostId(result.getInt(0)); posts.add(post); .... } while (result.moveToNext()); } 

This should now work correctly.

Note. Remember to call moveToFirst() which moves the cursor to the first record (implicitly positioned before the first line) and prepares it for reading. It is also a convenient method for checking if Cursor is valid or not.

Note 2: do not use column indices, you may just make a mistake when counting. Instead of using column names - this approach is usually recommended eq cursor.getColumnIndex("<columnName>")

+33
source

First you pointed the cursor to the cursor. Try it below

  String getPost = "SELECT * FROM " + TABLE_POST + ";"; Cursor result = getReadableDatabase().rawQuery(getPost, null); List posts = new ArrayList();{ Log.d("count", result.getCount() + " post rows"); if (result .moveToFirst()) { do { Post post = new Post(); post.setPostId(result.getInt(0)); posts.add(post); .... } while (result .moveToNext()); } result .close(); 
+6
source

This happens if our Cursor has data, but does not yet indicate the first index in the data list.

To avoid this exception, you must first check:

 if (!cursor.moveToFirst()) cursor.moveToFirst(); 

And then follow up.

+5
source

That was enough for me

 result.moveToFirst() do { ... ... }while(result .moveToNext()); 
0
source

Source: https://habr.com/ru/post/958056/


All Articles