将高分记录插入SQLite数据库,nullpointer - Android(java)

时间:2012-12-26 18:15:14

标签: android database sqlite sqliteopenhelper

我已修剪代码以尝试仅包含所需的代码段。我正在制作一个琐事游戏,当游戏完成时,Results.java显示并显示当前游戏的统计数据,并将完成的游戏发送到高分(SQLite数据库)。当调用Results.java类时,它会给出一个nullpointer异常(我在NP结果的位置注释了它)。

这是我第一次尝试在我的一个项目中创建和实现SQLite数据库。关于代码结构的任何其他建议或我的数据库代码看起来错误的其他内容非常感谢!

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper { 

    //variables

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

    public void createDatabase() throws IOException {
        boolean dbExist = checkDatabase();
        if(dbExist) {
            //Do nothing - DB already exists.
        } else {
            myDB.execSQL("CREATE TABLE " + TABLE + " ("
                            + RANK + " INTEGER,"
                            + SCORE + " LONG,"
                            + PERCENTAGE + " INTEGER"
                            + ");");
        }
    }

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

        return myDB.insert(DB_NAME, null, values);  //Line 56
    }

                //other methods...

    public void onCreate(SQLiteDatabase db) {}
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}

Results.java

public class Results extends Activity {

    DatabaseHelper dh;

    int rank;
    long score;

    @Override
    public void onCreate(Bundle savedInstanceState) {

                    //...
                    dh = new DatabaseHelper(this);
                    score = getIntent().getLongExtra("score", -1);

                    //...
    }

    public void showResults() {

        //...

        percentage = 10 * cAnswers;

                          //...

        dh.insert(1, score, percentage);  //Line 100
    }

    public void restart() {
        //...
    }
}

编辑:NullPointer错误消失了,但显示了一个新的LogCat输出,可以在下面找到。

LogCat输出

12-18 19:47:48.280: E/SQLiteLog(1527): (1) no such table: highscoresDatabase
12-18 19:47:48.370: E/SQLiteDatabase(1527): Error inserting percentageX=30 scoreX=827 rankX=1
12-18 19:47:48.370: E/SQLiteDatabase(1527): android.database.sqlite.SQLiteException: no such table: highscoresDatabase (code 1): , while compiling: INSERT INTO highscoresDatabase(percentageX,scoreX,rankX) VALUES (?,?,?)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1467)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at com.example.test.DatabaseHelper.insert(DatabaseHelper.java:56)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at com.example.test.Results.showResults(Results.java:100)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at com.example.test.Results.onCreate(Results.java:50)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.app.Activity.performCreate(Activity.java:5008)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.os.Looper.loop(Looper.java:137)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at java.lang.reflect.Method.invokeNative(Native Method)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at java.lang.reflect.Method.invoke(Method.java:511)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-18 19:47:48.370: E/SQLiteDatabase(1527):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:4)

你不能把

DatabaseHelper dh = new DatabaseHelper(this);

在字段声明空间中。这是因为您至少this或其他Activity方法无法访问onCreate上下文'。尝试将引用this的行移到onCreate内。

答案 1 :(得分:4)

您只能在调用onCreate()后使用您的活动上下文,否则它无效。只需将您的代码更改为:

DatabaseHelper db;

@Override
public void onCreate(Bundle savedInstanceState) {
    ...
    db = new DatabaseHelper(this);

添加

no such table: highscoresDatabase

您使用了错误的字符串,请在第56行使用TABLE代替DB_NAME

return myDB.insert(TABLE, null, values); // Line 56