-1

I got this exception when i run the project, here is part of main activity

  private void fillData() {
    // Get all of the notes from the database and create the item list
    Cursor notesCursor = mDbHelper.fetchAllNotes();

    if (notesCursor != null) {
        notesCursor.moveToFirst();
        do {
            messagesList.add(notesCursor.getString(notesCursor
                    .getColumnIndex(DbAdapter.KEY_FIELD2)));
            Log.e("Test", notesCursor.getString(notesCursor
                    .getColumnIndex(DbAdapter.KEY_FIELD2)));

        } while (notesCursor.moveToNext());
    }
    customAdapter.notifyDataSetChanged();
    // setListAdapter(notes);
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

First of all, you should test notesCursor != null AND notesCursor.moveToNext() (which returns a boolean). Having a cursor not null doesn't mean it has results to give. And moreover, you use the do/while loop, which means you suppose you'll always have as leats one result...

I would say you should use this instead:

private void fillData() {
    // Get all of the notes from the database and create the item list
    Cursor notesCursor = mDbHelper.fetchAllNotes();

    if (notesCursor != null) {
        while(notesCursor.moveToNext())
        {
            messagesList.add(notesCursor.getString(notesCursor
                    .getColumnIndex(DbAdapter.KEY_FIELD2)));
            Log.e("Test", notesCursor.getString(notesCursor
                    .getColumnIndex(DbAdapter.KEY_FIELD2)));
        }
        customAdapter.notifyDataSetChanged();
    }
}

And I also think you should only call notifyDataSetChanged() if some modifications were done: so move it INSIDE your if clause: You don't have to ask your system to re-calculate your ListView if nothing as changed...

Typically, your error may occur when calling moveToNext: Your cursor doesn't have any result but you ask it to move to next... Next of what? If no result, no way to move to next...

Jeje Doudou
  • 1,707
  • 1
  • 16
  • 24
0

SQliteDatabase.query() returns a Cursor object, which is positioned before the first entry. Cursor is never null but it can be empty. Your query() will either return a Cursor object or it will throw an exception. It will never return null.

boolean moveToFirst():

returns false if cursor is empty otherwise it will move cursor to the first row of the result and returns true.

You can check something like this:

notesCursor.moveToFirst();

while(notesCursor.moveToNext())
        {
            messagesList.add(notesCursor.getString(notesCursor
                    .getColumnIndex(DbAdapter.KEY_FIELD2)));
            Log.e("Test", notesCursor.getString(notesCursor
                    .getColumnIndex(DbAdapter.KEY_FIELD2)));
        }
        customAdapter.notifyDataSetChanged();
    }
Zohra Khan
  • 5,182
  • 4
  • 25
  • 34
  • Sure it can be null see this thread: http://stackoverflow.com/questions/13080540/what-causes-androids-contentresolver-query-to-return-null – Jeje Doudou Apr 16 '14 at 11:03