SQLite cursor.getString()NullPointerException

时间:2012-02-11 21:34:26

标签: android sqlite cursor nullpointerexception

当我尝试从Android中的SQLiteDatabase获取数据时,我收到了NullPointerException。我正在从原始查询创建一个Cursor,并调用cur.getString(index),但这会导致NullPointerException:

SQLiteDatabase db = dbHelper.getReadableDatabase();
    Cursor cur = db.rawQuery("SELECT Campaign, sum(Score) AS Score, Player FROM Levels GROUP BY Campaign", null);

    final int length = cur.getCount();
    names = new CampaignListItem[length];

    if (cur.moveToFirst()) {
        int row = 0;
        do {
            names[row].name = cur.getString(0); <--NPE
            names[row].score = cur.getInt(1);
            names[row].player = cur.getString(2);
            row++;
        } while (cur.moveToLast());
    }
    cur.close();
    db.close();  

我已记录curnames,但这些似乎不是空的。我做错了吗?

1 个答案:

答案 0 :(得分:2)

您的数组中没有任何CampaignListItems,因为您只构建数组但不添加任何对象。因此名称[row]为空。