如果我清除我的应用程序的数据然后运行它,它工作正常。如果我重新打开或重新运行我的应用程序,它将强制关闭并抛出一个java.lang.NullPointerException
? 错误在这部分代码中
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + DB_TBL + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_LEVEL + " INTEGER NOT NULL, " +
KEY_HEALTH + " INTEGER NOT NULL, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_CRIT + " INTEGER NOT NULL, " +
KEY_CRIT_RANGE + " INTEGER NOT NULL, " +
KEY_CRIT_INC + " INTEGER NOT NULL, " +
KEY_HIT_RANGE + " INTEGER NOT NULL, " +
KEY_HIT_INC + " INTEGER NOT NULL, " +
KEY_CHAR_IMG + " INTEGER NOT NULL, " +
KEY_TOTAL_XP + " INTEGER NOT NULL, " +
KEY_XP + " INTEGER NOT NULL, " +
KEY_XP_NEEDED + " INTEGER NOT NULL, " +
KEY_COINS + " INTEGER NOT NULL, " +
KEY_SMALL_POTS + " INTEGER NOT NULL, " +
KEY_LARGE_POTS + " INTEGER NOT NULL)"
);
values = new ContentValues();
values.put(KEY_NAME, "User1");
values.put( KEY_LEVEL, 1);
values.put(KEY_HEALTH, 10);
values.put(KEY_CRIT, 5);
values.put(KEY_CRIT_RANGE, 2);
values.put(KEY_CRIT_INC, 2);
values.put(KEY_HIT_RANGE, 2);
values.put(KEY_HIT_INC, 0);
values.put(KEY_TOTAL_XP, 0);
values.put(KEY_XP, 0);
values.put(KEY_XP_NEEDED, 5);
values.put(KEY_COINS, 5);
values.put(KEY_SMALL_POTS, 0);
values.put(KEY_LARGE_POTS, 0);
values.put(KEY_CHAR_IMG, 1);
db.insert(DB_TBL, null, values);
}
对此的java.lang.NullPointerException
要点
public int getLevel() {
open();
int level = values.getAsInteger(KEY_LEVEL);
close();
return level;
}
这是我的堆栈跟踪
01-04 14:45:45.694: W/dalvikvm(20858): threadid=9: thread exiting with uncaught exception (group=0x4001e578)
01-04 14:45:45.694: E/AndroidRuntime(20858): FATAL EXCEPTION: UpdateThread
01-04 14:45:45.694: E/AndroidRuntime(20858): java.lang.NullPointerException
01-04 14:45:45.694: E/AndroidRuntime(20858): at com.tanukiproductions.battleforchristmas.SQLiteTable.getLevel(SQLiteTable.java:110)
01-04 14:45:45.694: E/AndroidRuntime(20858): at com.tanukiproductions.battleforchristmas.Profile.<init>(Profile.java:57)
01-04 14:45:45.694: E/AndroidRuntime(20858): at com.tanukiproductions.battleforchristmas.MainMenuScene.onMenuItemClicked(MainMenuScene.java:37)
这是open()
和close()
public static void open() throws SQLException{
ourDatabase = ourHelper.getWritableDatabase();
}
public static void close() throws SQLException{
ourHelper.close();
}