SQLite 游标.getString() NullPointerException
当我尝试从 Android 中的 SQLiteDatabase 获取数据时,出现 NullPointerException。我正在从原始查询创建一个游标,并调用 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
,但是这些似乎不为空。我做错了什么吗?
I'm getting an NullPointerException when i'm trying to get data from my SQLiteDatabase in Android. I'm creating a Cursor from a raw query, and call cur.getString(index), however this results in a 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();
I logged cur
, and names
, however these don't seem to be Null. Am I doing something wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的数组中没有任何 CampaignListItems,因为您只构建了数组,但没有在其中添加任何对象。因此,names[row] 为空。
You don't have any CampaignListItems in your array, since you only build the array but don't add any objects there. Therefore names[row] is null.