gpt4 book ai didi

java - 在 sqlite 上插入时 SQLite android NullPointerException

转载 作者:行者123 更新时间:2023-12-01 12:51:41 31 4
gpt4 key购买 nike

我知道存在类似的问题,但我发现的所有问题都与没有 SQLiteDatabase 实例有关。

我的 onCreate 方法中创建数据库的表定义之一如下所示:

db.execSQL("CREATE TABLE offers(" +
" game_state_id INTEGER NOT NULL REFERENCES game_states (id)," +
" city_id INTEGER NOT NULL REFERENCES cities (id)," +
" item_id INTEGER NOT NULL REFERENCES items (id)," +
" sell_price INTEGER NOT NULL," +
" buy_price INTEGER NOT NULL," +
" amount INTEGER NOT NULL DEFAULT 0," +
" UNIQUE (game_state_id, city_id, item_id)" +
")");

它正在工作,数据库看起来很好。

public long createGameState() {
SQLiteDatabase db = this.getWritableDatabase();

ContentValues values = new ContentValues();
values.put("name", "default");
values.put("current_city_id", getInitialCityId());
// it could return -1 if insert didn't work
long res = db.insert("game_states", null, values);

List<City> cities = loadCities();
List<Item> items = loadItems();
Log.d("Kupiec", "Itemki: "+String.valueOf(items.size())+" | Miasta: "+String.valueOf(cities.size()));

for (City city : cities) {
for (Item item : items) {
ContentValues values2 = new ContentValues();
values2.put("item_id", item.id);
values2.put("city_id", city.id);
values2.put("game_state_id", res);
values2.put("buy_price", Utils.getRandom(100, 125));
values2.put("sell_price", Utils.getRandom(75, 100));
values2.put("amount", Utils.getRandom(15, 30));
Log.d("Kupiec", "offer_values: "+values2.toString());
db.insert("offers", null, values2);
}
}

db.close();
return res;
}

有趣的是,第一次插入到 game_states 表中工作正常,但是它在循环中的第一次插入时中断。

06-12 13:28:03.872      630-630/com.mippit.kupiec E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3044)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3039)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.database.sqlite.SQLiteStatement.releaseAndUnlock(SQLiteStatement.java:290)
at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:115)
at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1718)
at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1591)
at com.mippit.kupiec.Repository.createGameState(Repository.java:182)
at com.mippit.kupiec.Repository.createNewGame(Repository.java:153)
at com.mippit.kupiec.MainActivity.clicked_new_game(MainActivity.java:42)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3039)
at android.view.View.performClick(View.java:3511)
at android.view.View$PerformClick.run(View.java:14105)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)

发生什么事了?出现此错误的第 182 行是 db.insert("offers", null,values2);

的行

我得到了很好的答案,但作者删除了它,可能是因为他得到了 2 票反对(不是我!)。 如果有人可以将其退回,请这样做,我很乐意将其标记为已接受。

最后,正确的答案是try {...} catch {}这个NullPointerException并再次加载数据库。我不明白为什么会发生这种情况,因为我认为 db 将打开,直到我明确地说 db.close(),但是,它正在工作。

最佳答案

我发现问题是您已经在具有不同表的数据库中插入了值,并且在不关闭它的情况下尝试再次插入。所以冲突是存在的。替换 for 循环语句

db.insert("offers", null, values2);

db.insertWithOnConflict("table name", null, "values", SQLiteDatabase.CONFLICT_IGNORE); 

关于java - 在 sqlite 上插入时 SQLite android NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24183425/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com