1

I am getting a NullPointerException in my SQLite database. I am calling the DatabaseHelper class in Results.java by doing this:

public class Results extends Activity {
    ...
    DatabaseHelper dh;

    public void onCreate(Bundle savedInstanceState) {
    ...
    dh = new DatabaseHelper(this);
    }

    public void showResults() {
        ...
        dh.insert(1, score, percentage);
    }
}

DatabaseHelper.java

public class DatabaseHelper extends SQLiteOpenHelper { 

    private static final int DATABASE_VERSION = 1; 
    private static final String DB_NAME = "test1"; 
    private static final String DB_PATH = "/data/data/com.example.test/databases/";
    private static final String TABLE = "HighscoresList"; 

    // Table columns names. 
    private static final String RANK = "rank"; 
    private static final String SCORE = "score"; 
    private static final String PERCENTAGE = "percentage";

    private SQLiteDatabase myDB;

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

    //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(TABLE, null, values);
    }

    //Delete record.
    public boolean delete(long score) {
        //Need this?
        return true;
    }

    public void openDatabase() throws SQLException {
        //Open the database.
        String myPath = DB_PATH + DB_NAME;
        myDB = SQLiteDatabase.openDatabase(myPath,  null,  SQLiteDatabase.OPEN_READONLY);
    }

    public SQLiteDatabase getDatabase(){
        return this.myDB;
    }

    public synchronized void close() {
        if(myDB != null) {
            myDB.close();
        }
        super.close();
    }

    public void onCreate(SQLiteDatabase db) {
        myDB.execSQL("CREATE TABLE " + TABLE + " ("  //Line 62
                + RANK + " INTEGER,"
                + SCORE + " LONG,"
                + PERCENTAGE + " INTEGER"
                + ");");
    }
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {}
}

LogCat output

12-18 22:01:53.210: E/AndroidRuntime(6313): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Results}: java.lang.NullPointerException
12-18 22:01:53.210: E/AndroidRuntime(6313):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-18 22:01:53.210: E/AndroidRuntime(6313):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-18 22:01:53.210: E/AndroidRuntime(6313):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-18 22:01:53.210: E/AndroidRuntime(6313):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-18 22:01:53.210: E/AndroidRuntime(6313):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-18 22:01:53.210: E/AndroidRuntime(6313):     at android.os.Looper.loop(Looper.java:137)
12-18 22:01:53.210: E/AndroidRuntime(6313):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-18 22:01:53.210: E/AndroidRuntime(6313):     at java.lang.reflect.Method.invokeNative(Native Method)
12-18 22:01:53.210: E/AndroidRuntime(6313):     at java.lang.reflect.Method.invoke(Method.java:511)
12-18 22:01:53.210: E/AndroidRuntime(6313):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-18 22:01:53.210: E/AndroidRuntime(6313):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-18 22:01:53.210: E/AndroidRuntime(6313):     at dalvik.system.NativeStart.main(Native Method)
12-18 22:01:53.210: E/AndroidRuntime(6313): Caused by: java.lang.NullPointerException
12-18 22:01:53.210: E/AndroidRuntime(6313):     at com.example.test.DatabaseHelper.onCreate(DatabaseHelper.java:62)
12-18 22:01:53.210: E/AndroidRuntime(6313):     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
12-18 22:01:53.210: E/AndroidRuntime(6313):     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
12-18 22:01:53.210: E/AndroidRuntime(6313):     at com.example.test.DatabaseHelper.<init>(DatabaseHelper.java:25)
12-18 22:01:53.210: E/AndroidRuntime(6313):     at com.example.test.Results.onCreate(Results.java:33)
12-18 22:01:53.210: E/AndroidRuntime(6313):     at android.app.Activity.performCreate(Activity.java:5008)
12-18 22:01:53.210: E/AndroidRuntime(6313):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-18 22:01:53.210: E/AndroidRuntime(6313):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-18 22:01:53.210: E/AndroidRuntime(6313):     ... 11 more

What is causing the NPE?

Also, any help on code structure for this SQLite highscores I am trying to implement would be helpful as well.

4

0 回答 0