0

I am trying to access sqlite database in my app and getting this exception:

12-27 11:32:12.760: E/Exception:(746): java.lang.IllegalStateException: attempt to acquire a reference on a close SQLiteClosable Exception occured in ContactListOfNumbersForWhichRuleIsAlreadySpecified() of DatabaseHandlerRule.java

I am using this code:

public ArrayList<String> ContactListOfNumbersForWhichRuleIsAlreadySpecified(DatabaseHandlerRule Activity) 
    {
        ContactRule contact = null;
        Cursor cursor =  null;
        SQLiteDatabase db =  null;
        ArrayList<String> contactList =  null;
        try
        {
            contactList = new ArrayList<String>();

            //      SQLiteActivity1.ReadingAllContactsRule(SplashActivity.s_dbRule);

            String selectQuery = "SELECT  * FROM " + TABLE_CONTACTS + " WHERE "+KEY_NAME+" !='"
            +"abcde#$%&*()@#$%"+"'"+" AND "+KEY_PH_NO+"!='"+"abcde#$%&*()@#$%"+"'"+" AND "+KEY_DATE+" ='"+"0"+"'";

            db = this.getReadableDatabase();

            if (!db.isOpen()) {
                db = SQLiteDatabase.openDatabase(
                        "/data/data/com.velosys.smsManager/databases/rulesManager",
                        null, SQLiteDatabase.OPEN_READWRITE);
            }

            cursor = db.rawQuery(selectQuery, null);

            // looping through all rows and adding to list
            if (cursor.moveToFirst()) {
                do {
                    contact = new ContactRule();
                    contact.setID(Integer.parseInt(cursor.getString(0)));
                    contact.setName(cursor.getString(1));
                    contact.setPhoneNumber(cursor.getString(2));
                    contact.setFolderName(cursor.getString(3));
                    contact.setParentFolderAddress(cursor.getString(4));
                    contact.setTime(cursor.getLong(5));
                    contact.setDate(cursor.getLong(6));
                    // Adding contact to list
                    contactList.add(contact.getName());
                } while (cursor.moveToNext());
            }
            else if(!cursor.moveToFirst())
            {
                Log.e("Message: ","Rule is not specified for even a single number in database");
                return contactList;             
            }
        }
        catch(Exception e)
        {
            Log.e("Exception: ",e+" Exception occured in ContactListOfNumbersForWhichRuleIsAlreadySpecified() of DatabaseHandlerRule.java");
        }
        finally
        {   
            if(cursor != null && !cursor.isClosed())
                cursor.close(); 
            if(db != null && db.isOpen())
                db.close();
        }
        return contactList;
    }

I have searched for the reasons behind this exception but nothing implies to my case:

  1. I am not trying to write but read the database.Hence no question arises here about concurrency.
  2. I am using instance of Database helper class in db = this.getReadableDatabase();
  3. I am closing database properly each and everytime i open it.

Please help me.Thanks in advance.

Edit:

Its strange.After removing finally and putting the code without finallly block,everything is working fine.Can anyone please tell me the reason behind this?

          //finally
            {   
                if(cursor != null && !cursor.isClosed())
                    cursor.close(); 
                if(db != null && db.isOpen())
                    db.close();
            }
6
  • Which line causes the exception? Commented Dec 27, 2012 at 6:17
  • Can you tell at which line you are getting this error.? Commented Dec 27, 2012 at 6:18
  • I can't say because while debugging,i am not able to produce the error.Everything is working fine while debugging. Commented Dec 27, 2012 at 6:30
  • You can if you use e.printStackTrace() Commented Dec 27, 2012 at 6:34
  • then how could you see the exception as you mentioned above.? Commented Dec 27, 2012 at 6:37

3 Answers 3

3

Please remove finally block. in that you have close db and cursor so it this error comes.

Sign up to request clarification or add additional context in comments.

5 Comments

I am supposed to close the cursor and database after completion of my work.Is not it?If i am closing them in finally clause then what is the problem?Can you please tell me?
in that you have close same db and cursor on the file DatabaseHandlerRule.java
@VatsalShah please check the edit and please put some light on reason behind it.
in finally block everytime cursor and db will be close and open thats why. instead of that completing in finally. after while loop complete close it.
@VatsalShah: Appreciated.!:)
0

In the statement db = this.getReadableDatabase();, I'm not sure of what context you are using. So declare a global context like Context context = this; then call that statement as db = context.getReadableDatabase();

2 Comments

Using this i am using the context of database open helper class i.e DatabaseHandlerRule. this = DatabaseHandlerRule.this;
I think the method I suggested is more direct. Please try it out and say. Because I'm not familiar with your method of referencing :(
0

Its strange.After removing finally and putting the code without finallly block,everything is working fine.Can anyone please tell me the reason behind this?

  //finally
            {   
                if(cursor != null && !cursor.isClosed())
                    cursor.close(); 
                if(db != null && db.isOpen())
                    db.close();
            }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.