当我尝试从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();
我已记录cur
和names
,但这些似乎不是空的。我做错了吗?
答案 0 :(得分:2)
您的数组中没有任何CampaignListItems,因为您只构建数组但不添加任何对象。因此名称[row]为空。