Search code examples
androidsqlitecursor

Android : Error on MoveToNext()


I'm trying to use SQLite database on Android to stock an object Discuss (ID, USER) on a table which is (ID, ID2, USER).

I have set my database, and put some items (5) on it. I checked with an ADB SHELL and SQLITE3 command.

When I want to get all items with the following method :

public ArrayList<Discuss> getAllDiscuss(){
    ArrayList<Discuss> discussList = new ArrayList<Discuss>();
    Cursor cursor = bdd.query(TABLE_DISCUSS, new String[] {COL_DISCUSS_ID, COL_DISCUSS_ID_SERVER, COL_ID_OTHERUSER}, null, null, null, null, null);
    try {
        if (cursor.moveToFirst()) {
            do {
                discussList.add(cursorToDiscuss(cursor));
            } while (cursor.moveToNext());
        }
    } finally {
        try { cursor.close(); } catch (Exception ignore) {}
    }
    return discussList;
}

I have this error :

02-04 23:48:25.333: E/AndroidRuntime(2122): FATAL EXCEPTION: main
02-04 23:48:25.333: E/AndroidRuntime(2122): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gam/com.gam.MainActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT ID, Id_Server, Id_OtherUser FROM Table_Discuss) 
02-04 23:48:25.333: E/AndroidRuntime(2122):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at android.os.Looper.loop(Looper.java:137)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at android.app.ActivityThread.main(ActivityThread.java:4340)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at java.lang.reflect.Method.invokeNative(Native Method)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at java.lang.reflect.Method.invoke(Method.java:511)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at dalvik.system.NativeStart.main(Native Method)
02-04 23:48:25.333: E/AndroidRuntime(2122): Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT ID, Id_Server, Id_OtherUser FROM Table_Discuss) 
02-04 23:48:25.333: E/AndroidRuntime(2122):     at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:33)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:82)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:160)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at android.database.sqlite.SQLiteCursor.onMove(SQLiteCursor.java:143)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at android.database.AbstractCursor.moveToPosition(AbstractCursor.java:175)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at android.database.AbstractCursor.moveToNext(AbstractCursor.java:243)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at com.gam.sqlite.GamAppSqliteBDD.getAllDiscuss(GamAppSqliteBDD.java:242)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at com.gam.MainActivity.onCreate(MainActivity.java:116)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at android.app.Activity.performCreate(Activity.java:4465)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-04 23:48:25.333: E/AndroidRuntime(2122):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
02-04 23:48:25.333: E/AndroidRuntime(2122):     ... 11 more

The line 242 of GamAppSqliteBDD.java file is :

} while (cursor.moveToNext());

Can anyone help me to understand and solve this error ?

I have this error too if I use the following code :

    public ArrayList<Discuss> getAllDiscuss(){
        ArrayList<Discuss> discussList = new ArrayList<Discuss>();
        Cursor cursor = bdd.query(TABLE_DISCUSS, new String[] {COL_DISCUSS_ID, COL_DISCUSS_ID_SERVER, COL_ID_OTHERUSER}, null, null, null, null, null);
        try {
            if (cursor.moveToFirst()) {
//                do {
                    discussList.add(cursorToDiscuss(cursor));
                    discussList.add(cursorToDiscuss(cursor));
//                } while (cursor.moveToNext());
            }
        } finally {
            try { cursor.close(); } catch (Exception ignore) {}
        }
        return discussList;
    }

But this time at the second line : discussList.add(cursorToDiscuss(cursor));

Thanks for your reading !


Solution

  • [Not Recommended] I think the issue is closing the cursor. Just put that cursor.close() in comments and run again!!

    //cursor.close()
    

    [Recommended]

    The error is at line

    02-04 23:48:25.333: E/AndroidRuntime(2122): Caused by: java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT ID, Id_Server, Id_OtherUser FROM Table_Discuss) 
    

    It says that you are trying to open object which is already closed. In this case its cursor you are using. You probably have cursor in operation even after you have closed it.

    Getting your code in right order will play the trick ! Cheers