Search code examples
javaandroidnullpointerexceptioncursorsqliteopenhelper

Android Cursor NPE dumpCursorToString returns values


As the title says, I'm experiencing a NullPointerException which occurs at the first getString() in the below method. For debugging I have used DatabaseUtils.dumpCursorToString() to view the cursor and there are values within the cursor but between the if statement and the do statement, something changes. I use this same code in another method in a separate activity and it works flawlessly.

public ArrayList<String> getMusic() {
    helper = new DbHelper(context);
    db = helper.getReadableDatabase();

    String[] cols = new String[]{MUSIC_ID, MUSIC_COUNTRY, MUSIC_BLUES, MUSIC_ROCK, MUSIC_METAL,
            MUSIC_HIPHOP, MUSIC_CLASSICAL, MUSIC_PUNK, MUSIC_TECHNO, MUSIC_JAZZ, MUSIC_OTHER};

    Cursor c = db.query(TABLE_MUSIC_INT, cols, null, null, null, null, null);

    int count = c.getCount();
    Log.v("INTEREST CURSOR ", "Count: " + count); // log row count
        if (c.moveToFirst()) {
            Log.v("INTEREST CURSOR ", DatabaseUtils.dumpCursorToString(c)); // dump cursor values
            do {
                musicInt.add(c.getString(0));
                musicInt.add(c.getString(1));
                musicInt.add(c.getString(2));
                musicInt.add(c.getString(3));
                musicInt.add(c.getString(4));
                musicInt.add(c.getString(5));
                musicInt.add(c.getString(6));
                musicInt.add(c.getString(7));
                musicInt.add(c.getString(8));
                musicInt.add(c.getString(9));
                musicInt.add(c.getString(10));
            } while (c.moveToNext());
        } else {
            Log.e("DB_GET_DATA ", " DB_GET_DATA == null");
        }
    c.close();

    return musicInt;
}

DatabaseUtils.dumpCursorToString(c) output:

01-15 19:57:01.201 6247-6247/us.enyi.enyi D/DB OPEN READ: Successful
01-15 19:57:20.440 6247-6247/us.enyi.enyi V/INTEREST CURSOR: Count: 1
01-15 19:57:27.897 6247-6247/us.enyi.enyi V/INTEREST CURSOR: >>>>> Dumping cursor android.database.sqlite.SQLiteCursor@2f32900f
01-15 19:57:27.897 6247-6247/us.enyi.enyi V/INTEREST CURSOR: 0 {
01-15 19:57:27.897 6247-6247/us.enyi.enyi V/INTEREST CURSOR:    _id=1
01-15 19:57:27.897 6247-6247/us.enyi.enyi V/INTEREST CURSOR:    country=null
01-15 19:57:27.897 6247-6247/us.enyi.enyi V/INTEREST CURSOR:    blues=null
01-15 19:57:27.897 6247-6247/us.enyi.enyi V/INTEREST CURSOR:    rock=Rock
01-15 19:57:27.897 6247-6247/us.enyi.enyi V/INTEREST CURSOR:    metal=Metal
01-15 19:57:27.897 6247-6247/us.enyi.enyi V/INTEREST CURSOR:    hiphop=HipHop
01-15 19:57:27.897 6247-6247/us.enyi.enyi V/INTEREST CURSOR:    classical=Classical
01-15 19:57:27.897 6247-6247/us.enyi.enyi V/INTEREST CURSOR:    punk=null
01-15 19:57:27.897 6247-6247/us.enyi.enyi V/INTEREST CURSOR:    techno=Techno
01-15 19:57:27.897 6247-6247/us.enyi.enyi V/INTEREST CURSOR:    jazz=null
01-15 19:57:27.897 6247-6247/us.enyi.enyi V/INTEREST CURSOR:    musicother=Other
01-15 19:57:27.897 6247-6247/us.enyi.enyi V/INTEREST CURSOR: }
01-15 19:57:27.897 6247-6247/us.enyi.enyi V/INTEREST CURSOR: <<<<<

I've attempted other methods of retrieving the data from a cursor and read countless threads here on SO as well as within Android Docs, without finding anything with a helpful resolution. I don't quite understand why the cursor obviously has values but c.getString() fails on a NPE. Any assistance will be greatly appreciated.

Solution:

Failure to initialize musicInt caused the Cursor to have nowhere to put the data it collected. Declared musicInt as:

ArrayList<String> musicInt;

Instead of:

ArrayList<String> musicInt = new ArrayList<>();

The lesson is ensure you initialize your variables appropriately. You'll pull your hair out like I did otherwise.


Solution

  • I don't see where you declare (or initialize) musicInt. That might be what's causing your NPE.