アプリのデータをクリアして実行すると、正常に機能します。アプリを再度開いたり再実行したりすると、強制的に閉じてスローし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();
}