gpt4 book ai didi

java - 游标 RawQuery 上的 NullPointerException - Android

转载 作者:行者123 更新时间:2023-12-01 07:11:05 25 4
gpt4 key购买 nike

我正在尝试创建一个数据库,插入到数据库中,从数据库中提取内容并将内容(长)显示到 TextView。

我认为我正在正确创建并插入数据库。在我的 getScore() 方法中,我收到 NullPointerException。

任何有关代码结构的帮助也非常感谢!预先感谢您。

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper { 

SQLiteDatabase db;

// Table columns names.
private static final String SCORE = "score";

public DatabaseHelper(Context context) {
super(context, DB_NAME, null, DATABASE_VERSION);
}

public SQLiteDatabase openDB() {
db = this.getWritableDatabase();
return db;
}

public long getScore(SQLiteDatabase db) {
//Cursor c = db.rawQuery("SELECT " + SCORE + " FROM " + TABLE + " WHERE " + SCORE + " = " + 11 + ";", null); //Line 45
Cursor c = db.rawQuery("SELECT " + SCORE + " FROM " + TABLE + ";", null); //Line 39
long i = 0;
if(c.getCount() == 0) {
i = 333;
} else if (c.getCount() == 1) {
i = 444;
} else {
i = 888;
/*c.moveToFirst();
int columnIndex = c.getInt(c.getColumnIndex("SCORE"));
if(columnIndex != -1) {
i = c.getLong(columnIndex);
} else {
i = 999;
}*/
}
c.close();
return i;
}

//Insert new record.
public long insert(long score, int percentage) {
ContentValues values = new ContentValues();
values.put(SCORE, score);
values.put(PERCENTAGE, percentage);

return db.insert(TABLE, null, values);
}

public synchronized void closeDB(SQLiteDatabase db) {
if(db != null) {
db.close();
}
super.close();
}

public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE + " ("
+ RANK + " INTEGER PRIMARY KEY AUTOINCREMENT,"
+ SCORE + " LONG,"
+ PERCENTAGE + " INTEGER"
+ ");");
}
}

Highscores.java

public class Highscores extends Activity {

DatabaseHelper dh;
SQLiteDatabase db;
long scores;

TextView score;

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.highscoresmain);

dh = new DatabaseHelper(this);
dh.openDB();
long x = 11;
int y = 22;

TextView score = (TextView)findViewById(R.id.score);
TextView r1s = (TextView)findViewById(R.id.r1s);

dh.insert(x, y);
scores = dh.getScore(db); //Line 56
score.setText("Score Column - TEST");
r1s.setText("" + score);

dh.closeDB(db);
}
}

LogCat 输出

01-03 17:36:01.172: E/AndroidRuntime(1941): FATAL EXCEPTION: main
01-03 17:36:01.172: E/AndroidRuntime(1941): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Highscores}: java.lang.NullPointerException
01-03 17:36:01.172: E/AndroidRuntime(1941): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
01-03 17:36:01.172: E/AndroidRuntime(1941): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
01-03 17:36:01.172: E/AndroidRuntime(1941): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-03 17:36:01.172: E/AndroidRuntime(1941): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
01-03 17:36:01.172: E/AndroidRuntime(1941): at android.os.Handler.dispatchMessage(Handler.java:99)
01-03 17:36:01.172: E/AndroidRuntime(1941): at android.os.Looper.loop(Looper.java:137)
01-03 17:36:01.172: E/AndroidRuntime(1941): at android.app.ActivityThread.main(ActivityThread.java:5039)
01-03 17:36:01.172: E/AndroidRuntime(1941): at java.lang.reflect.Method.invokeNative(Native Method)
01-03 17:36:01.172: E/AndroidRuntime(1941): at java.lang.reflect.Method.invoke(Method.java:511)
01-03 17:36:01.172: E/AndroidRuntime(1941): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
01-03 17:36:01.172: E/AndroidRuntime(1941): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
01-03 17:36:01.172: E/AndroidRuntime(1941): at dalvik.system.NativeStart.main(Native Method)
01-03 17:36:01.172: E/AndroidRuntime(1941): Caused by: java.lang.NullPointerException
01-03 17:36:01.172: E/AndroidRuntime(1941): at com.example.test.DatabaseHelper.getScore(DatabaseHelper.java:39)
01-03 17:36:01.172: E/AndroidRuntime(1941): at com.example.test.Highscores.onCreate(Highscores.java:56)
01-03 17:36:01.172: E/AndroidRuntime(1941): at android.app.Activity.performCreate(Activity.java:5104)
01-03 17:36:01.172: E/AndroidRuntime(1941): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
01-03 17:36:01.172: E/AndroidRuntime(1941): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
01-03 17:36:01.172: E/AndroidRuntime(1941): ... 11 more

编辑:新的getScore()方法。

public long getScore() {
Cursor c = db.rawQuery("SELECT " + SCORE + " FROM " + TABLE + " WHERE " + SCORE + " = " + 11 + ";", null);
//Cursor c = db.rawQuery("SELECT " + SCORE + " FROM " + TABLE + ";", null);
long i = 0;
if(c.getCount() == 0) {
i = 333;
} else if (c.getCount() == 1) {
i = 444;
} else if (c.getCount() == 2) {
i = 555;
} else {
i = 666;
/*c.moveToFirst();
int columnIndex = c.getInt(c.getColumnIndex("SCORE"));
if(columnIndex != -1) {
i = c.getLong(columnIndex);
} else {
i = 999;
}*/
}
c.close();
return i;
}

这仍然返回值为 666 的 i。

最佳答案

您将在此处覆盖本地 db 值: public long getScore(SQLiteDatabase db) { ... 。删除该参数,它应该可以工作。

关于java - 游标 RawQuery 上的 NullPointerException - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14144468/

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