onkar
onkar

Reputation: 4547

java.lang.IllegalStateException: attempt to re-open an already-closed object(Tried closing )

public int getRecordsCount() {
        String countQuery = "SELECT  * FROM " + TABLE_LOGIN;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        if(cursor != null && !cursor.isClosed()){
            cursor.close();
        }   
        // return count

        return cursor.getCount();
    }

I am trying to get the totalnumber of records in the database, but the database is getting crashed everytime with java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT * FROM login) . Please help me with the error

  03-05 22:23:14.208: E/AndroidRuntime(4988): FATAL EXCEPTION: main
    03-05 22:23:14.208: E/AndroidRuntime(4988): java.lang.IllegalStateException: attempt to re-open an already-closed object: android.database.sqlite.SQLiteQuery (mSql = SELECT  * FROM login) 
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:34)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:64)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:283)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.database.sqlite.SQLiteCursor.getCount(SQLiteCursor.java:264)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at com.ecomm.android.sqlite.DatabaseHandler.getRecordsCount(DatabaseHandler.java:123)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at com.ecomm.android.LaunchActivity.DataBaseImplementation(LaunchActivity.java:120)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at com.ecomm.android.LaunchActivity.onClick(LaunchActivity.java:98)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.view.View.performClick(View.java:2408)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.view.View$PerformClick.run(View.java:8816)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.os.Handler.handleCallback(Handler.java:587)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.os.Handler.dispatchMessage(Handler.java:92)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.os.Looper.loop(Looper.java:123)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at android.app.ActivityThread.main(ActivityThread.java:4627)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at java.lang.reflect.Method.invokeNative(Native Method)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at java.lang.reflect.Method.invoke(Method.java:521)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
    03-05 22:23:14.208: E/AndroidRuntime(4988):     at dalvik.system.NativeStart.main(Native Method)
    03-05 22:23:15.608: I/binder_sample(4988): [android.app.IActivityManager,2,1395,com.ecomm.android,100]
    03-05 22:23:15.608: I/binder_sample(4988): Unknown binary event type 110
    03-05 22:23:15.608: I/binder_sample(4988): Binary log entry conversion failed

Upvotes: 23

Views: 50793

Answers (5)

Prajwal Waingankar
Prajwal Waingankar

Reputation: 2710

For me issue was that I was closing the db instance in the finally block for the update query.

For people who are not able to fix using the above solution, try checking your update query.

Upvotes: 0

Michał Z.
Michał Z.

Reputation: 4119

You should call getCount() before you close your cursor.

Move:

if(cursor != null && !cursor.isClosed()){
    cursor.close();
} 

below:

 cursor.getCount();

like this:

public int getRecordsCount() {
    int count = 0;
    String countQuery = "SELECT  * FROM " + TABLE_LOGIN;

    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery(countQuery, null);



    if(cursor != null && !cursor.isClosed()){
        count = cursor.getCount();
        cursor.close();
    }   
    return count;
}

Upvotes: 29

Bond James
Bond James

Reputation: 33

remove the cursor.close() statement

Upvotes: 2

craned
craned

Reputation: 3051

I know this is old, but my problem ended up being that in onCreate(), I was calling db.close() after executing db.execSQL(TABLE_CREATE).

onCreate is automatically called after dbHelper.getReadableDatabase().

This caused it to crash because then it went to retrieve all the values from the database with an already-closed object. Once I removed db.close() from onCreate, it all worked.

Upvotes: 1

Simon Dorociak
Simon Dorociak

Reputation: 33505

java.lang.IllegalStateException: attempt to re-open an already-closed

Your error is thrown because you call cursor.getCount() on Cursor that you already close and this is not allowed.

So either try to use try-finally block where in finally block you close your Cursor or assign cursor.getCount() to int value and close Cursor immediately.

But i recommend to you use first approach:

public int getRecordsCount() {
   String countQuery = "SELECT * FROM " + TABLE_LOGIN;
   SQLiteDatabase db = this.getReadableDatabase();
   Cursor cursor = db.rawQuery(countQuery, null);
   int count = 0;
   try {
      if (cursor.moveToFirst())) {
         count = cursor.getCount();
      }
      return count;
   }
   finally {
      if (cursor != null) {
         cursor.close();
      }
   }
}

Upvotes: 5

Related Questions