Android Cursor NPE dumpCursorToString返回值

时间:2016-01-16 03:56:20

标签: java android nullpointerexception cursor sqliteopenhelper

正如标题所说,我遇到的NullPointerException出现在下面方法的第一个getString()。对于调试,我使用DatabaseUtils.dumpCursorToString()来查看游标,并且游标中有值,但在if语句和do语句之间有一些更改。我在另一个单独的活动中使用相同的代码,它可以完美地运行。

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)输出:

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: <<<<<

我尝试过从光标检索数据的其他方法,并在SO和Android文档中读取无数线程,但没有找到任何有用的解决方案。我不太明白为什么光标显然有值,但c.getString()在NPE上失败。任何帮助将不胜感激。

解决方案

无法初始化musicInt导致Cursor无处放置它收集的数据。声明musicInt为:

ArrayList<String> musicInt;

而不是:

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

本课程是确保您正确初始化变量。你会像我一样把你的头发拉出来。

1 个答案:

答案 0 :(得分:2)

我没有看到您声明(或初始化)musicInt的位置。这可能是导致你的NPE的原因。