3
即时通讯学习如何使用sqlite数据库,我试图在我制作的游戏中实现高分系统。阅读行的作品,当我尝试显示分数,但当我尝试使用相同的想法来读取值来比较它们在下面的方法 - 它给出一个空指针异常错误。奇怪的nullPointerException错误 - 数据库相关
private void checkIfHighScore(int currentScore) {
Cursor note;
int rowScore;
String moveName, moveScore, newName;
for (long i=1;i<6;i++){
note = mDbHelper.fetchNote(i);
startManagingCursor(note);
rowScore=Integer.parseInt(note.getString(note.getColumnIndexOrThrow(ScoresDbAdapter.KEY_BODY)));
if (currentScore<rowScore){
for (long j=5;j>i;j--){
note = mDbHelper.fetchNote(j-1);
startManagingCursor(note);
moveName=note.getString(note.getColumnIndexOrThrow(ScoresDbAdapter.KEY_TITLE));
moveScore=note.getString(note.getColumnIndexOrThrow(ScoresDbAdapter.KEY_BODY));
mDbHelper.updateNote(j, moveName, moveScore);
}
newName="a"; //for testing
mDbHelper.updateNote(i, newName, Integer.toString(currentScore));
break;
}
}
}
我知道错误是在for循环之后 - 但我不知道是什么导致它失败。我的数据库有5行数据(已验证)。 有谁知道是什么原因导致我的错误?一直在我的头上撞墙2小时。谢谢!
'mDbHelper'如何声明和初始化? – 2012-03-24 09:21:50
它可能是:'mDbHelper'未初始化,或'mDbHelper.fetchNote(i)'返回null(我们没有代码),请添加日志猫和更多代码,以便我们可以帮助 – 2012-03-24 09:29:14
mDbHelper = new ScoresDbAdapter(本); mDbHelper.open(); 也记住我使用相同的命令(fetchNote)以另一种方法从数据库中读取,所以它运行良好,极有可能。 – Zephyer 2012-03-24 09:43:05