• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Attempt to Finalize Cursor That has not been closed

Hi everyone, I am new to the forum. I ran accross a problem the other day. My application creates a database on the fly and also populates it. The problem is I get a "Attempt to Finalize Cursor That has not been closed" error in logcat which mainly refers to this function.
Code:
public static void populateFromDatabase(){
        /* if the database is empty just throw in the question */
        Cursor c = AppData.db.db.rawQuery("SELECT * FROM question", null);
        
        if (c != null && c.getCount() == 0){
            c.moveToFirst() ;
            int count = 0 ;
            while(!c.isAfterLast()){
                count++ ;
                c.moveToNext() ;
            }
            if (count == 0) {
                    // insert some rows

            }
        }
        Log.e("Create done", "Create done") ;
        AppData.idList = new ArrayList<Integer>() ;
        AppData.Q = new HashMap<Integer, String>() ;
        AppData.optA = new HashMap<Integer, String>() ;
        AppData.optB = new HashMap<Integer, String>() ;
        AppData.optC = new HashMap<Integer, String>() ;
        AppData.optD = new HashMap<Integer, String>() ;
        AppData.ans = new HashMap<Integer, String>() ;
        AppData.type = new HashMap<Integer, String>() ;
        int qSelected = 0 ;
        int j = 0 ;
        while (qSelected != 6){
            Random rand = new Random() ;
            int index = (rand.nextInt() % 16) ;
            if (index < 0) index *= 1 ;
            index += 1 ;
            if (AppData.Q.containsKey(index))
                continue ;
            
            c = AppData.db.db.rawQuery("SELECT * FROM question WHERE id=" + index, null) ;
            c.moveToFirst() ;
            
            while (!c.isAfterLast()){
                Integer id = c.getInt(0) ;
                String q = c.getString(1) ;
                String oa = c.getString(2) ;
                String ob = c.getString(3) ;
                String oc = c.getString(4) ;
                String od = c.getString(5) ;
                String ans = c.getString(6) ;
                String type = c.getString(7) ;
                
                AppData.idList.add(id) ;
                AppData.Q.put(id, q) ;
                AppData.optA.put(id, oa) ;
                AppData.optB.put(id, ob) ;
                AppData.optC.put(id, oc) ;
                AppData.optD.put(id, od) ;
                AppData.ans.put(id, ans) ;
                AppData.type.put(id, type) ;
                
                qSelected++ ;
                j++ ;
                //Toast.makeText(null, q + oa + ob + oc + od + ans, Toast.LENGTH_LONG) ;
                Log.e("qqq", id + " " + q + oa + ob + oc + od + ans) ;
                c.moveToNext() ;
                
            }
            
            
        }
        AppData.qCount = 6 ;
        c.close() ;

        
    }

My question is I am closing the cursor in all occasions so why this error ? Also note that i call this function only once in the program and the logcat error points to this, can some1 point out what I am doing wrong here.
Thanx in advancce
 
it looks like you have two cursors, both named c, one inside a while-loop that gets changed for each iteration of the while loop and has no calls to c.close().

I'd suspect this is the root of the problem.

You may want to name your second cursor to c2 or something and be sure to close it after each and every time you set it to a different rawQuery.

edit, also welcome to the forums! :)
 
Back
Top Bottom