Attempt to re-open an already-closed object: java.lang.IllegalStateException:?
Asked Answered
E

3

15

I know this question has asked many times in SO,but i couldn't figure out my exact problem.

I am using the following code to get the data from the database(Table1) and update another Table2 based on retrieval value. Its working fine in some android versions but when i gone to test with Android 4.0.3. I am geting this java.lang.IllegalStateException:?.attempt to re-open an already-closed object at sum_cursor.moveToNext();.

I am using this code in AsyncTask.

 /** Sum of total matched values*/
            Cursor sum_cursor = db.gettotalMatchvalue(this);
             if(sum_cursor!=null)
             {
                 sum_cursor.moveToFirst();
                 for(int j=0; j<sum_cursor.getCount();j++)
                 {    
                     float totalmatchedscore = sum_cursor.getInt(0);
                     float totalingredients = Float.parseFloat(sum_cursor.getString(sum_cursor.getColumnIndex(APPDatabase.CK_TOTALINCREDIENTS)));
                     /**average = totalscore/totalingredients*/
                     double average = totalmatchedscore/totalingredients;
                     int id = Integer.parseInt(sum_cursor.getString(sum_cursor.getColumnIndex(APPDatabase.CK_ID))); 

                 db.updateAverage(id, average); 
                 sum_cursor.moveToNext(); //Here is the problem
                 }  
             }   
             db.close();  

My update method coding

/** Update average */
public void updateAverage(int id,double average)
{
    SQLiteDatabase db = getWritableDatabase();
    ContentValues values = new ContentValues();
    values.put(CK_FINALVALUE,average);
    db.update(TABLE, values,CK_ID+" = "+id , null);   
}

What i am doing wrong here?

I know many of you come across this situation. Could you help me guys.

Thanks for your help.

Eustashe answered 24/7, 2012 at 14:55 Comment(3)
Maybe you have closed the database outside of the async task. Have you checked for that?Maximomaximum
Is the system that 4.0.3 is running on any faster than the other systems you're using, if so you could have some race conditions occurring.Maximomaximum
An observation: your code for looping over the cursor is more complex than it needs to be. See #10724270Crowfoot
C
23

You can't update a table while iterating over the results of a query. There are good reasons for this; what if the data you are adding would cause a change to the data you're iterating over? Your cursor would not return valid data.

Trying to store data back into the table in updateAverage() is causing the problem. You should just remember the average value during your loop, and then update it once at the end after you've finished looping over your cursor.

To further explain the exact error you're getting: the act of inserting new data is causing the database to close all the cursors which are currently open, as a safety measure. So when you call sum_cursor.moveToNext() after updating the average, the cursor is a bit surprised to find that it's already been closed.

Crowfoot answered 25/7, 2012 at 8:45 Comment(2)
Thanks for your fabulous information.. From your info what i got is store the average value in array or list and then update it in another loop.. Am i rightEustashe
thanks i will try it and let you know.. and thanks for your immediate support +1 for thatEustashe
T
2

What if you comment out db.updateAverage(id, average) ?

Tarpeia answered 24/7, 2012 at 15:4 Comment(6)
thanks for your answer.. actually db.updateAverage(id, average) is very important.. based on this update only i am going to next stepEustashe
@Vinoth I mean, if you comment it out does then the error go away?Tarpeia
sorry alex i couldn't test now because that phone was my friends.. i can test only tomorrow and let you know.. its really wiredEustashe
I mean, db.updateAverage seems to be the only place in your code of which no one except you can know.Tarpeia
@Vinoth Perhaps db.update invalidates your current Cursor. Anyway, db.update changes the table contents and you are still keeping the old cursor, that's incorrect.Tarpeia
@Alex thanks what can i do now.. have u got any idea shall i move sum_cursor.moveToNext before db.update.. will that workEustashe
F
0

You can achieve your goal with pure SQL it is faster and better from architecture point of View because all logic will be in one SQL transaction

REPLACE INTO table_where_to_put  SELECT *, (totalmatchedscore/totalingredients) as average  FROM table_with_input_data

Use REPLACE OR UPDATE

Fimbriation answered 18/9, 2014 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.