1

I am getting this error when trying to retrieve all the entries of a table on the local database:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.contactappca2/com.example.contactappca2.ViewActivity}: java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.

This is my method inside my DbAdapter class where I retrieve the entries:

public List<Contact>getAllContacts(){
    contacts = new ArrayList<Contact>();
    cursor = db.query(TABLE_NAME, new String [] {COLUMN_NAME, COLUMN_NUMBER, COLUMN_EMAIL, COLUMN_IMG}, null,null,null,null,null);
    if(cursor.moveToFirst()){
        do{
            c = new Contact();
            c.setName(cursor.getString(0), null);
            c.setNumber(cursor.getString(1));
            c.setEmail(cursor.getString(2));
            byte [] blob = cursor.getBlob(cursor.getColumnIndex(COLUMN_IMG));
            ByteArrayInputStream inputStream = new ByteArrayInputStream(blob);
            Bitmap b = BitmapFactory.decodeStream(inputStream); 
            c.setPicture(b);
            contacts.add(c);
        }while(cursor.moveToNext());
    }

    return contacts;
}

Any Idea what I am doing wrong? Many thanks.

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Javacadabra
  • 5,578
  • 15
  • 84
  • 152
  • 3
    my theory is that your query can't allocate all the memory it needs for the images called for to successfully generate that `Cursor`. try the method without requesting the `Blob` column and the subsequent methods involving it and see if you have any success. – mango Jan 18 '13 at 02:10
  • Ok, if that is the case is there a way I can resolve the memory issue? – Javacadabra Jan 18 '13 at 02:11
  • @mango I did as you suggested and the issue with the cursor was no longer a problem. What could be causing memory issues? – Javacadabra Jan 18 '13 at 02:15
  • Are you sure cursor is initialized already? This snippet shows your process for accessing the cursor, but doesn't show how/if you initialized it and/or your 'db'. – CodeShane Jan 18 '13 at 02:44

1 Answers1

0

well one thing to do is make sure your other Cursors are closed before you make this one (in order to release memory).

But you'll see much better results if you try to get your content out in chunks, much to the effect of an endlessly loading listview or something of the sort.

But i really have to say that if i was even running into an issue like this in the first place, i'd be ditching the idea of using SQLite to hold full blown objects like this. Much better would be to have these images stored in sd card or local memory and only save the image locations or at least names in the database, then manage your content in this manner.

mango
  • 5,577
  • 4
  • 29
  • 41