0

I am trying to implement a SQLite database for a highscores table. I am just testing it to see if my database creation and insertion is working with the below code. But I am getting a NullPointerException on the below lines.

Thank you in advance!

Results.java

public class Results extends Activity {
    DatabaseHelper dh;

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

    public void showResults() {
        dh.openDB();  
        dh.insert(1231423, 436346);  //Line 104
    }
}

DatabaseHelper.java

private static final String SCORE = "score"; 
private static final String PERCENTAGE = "percentage";
public static DatabaseHelper mSingleton = null;

public synchronized static DatabaseHelper getInstance(Context context) {
    if(mSingleton == null) {
        mSingleton = new DatabaseHelper(context.getApplicationContext());
    }
    return mSingleton;
}

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

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);  //Line 91
}

LogCat output

01-02 17:56:47.609: E/AndroidRuntime(1322): FATAL EXCEPTION: main
01-02 17:56:47.609: E/AndroidRuntime(1322): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.Results}: java.lang.NullPointerException
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.os.Looper.loop(Looper.java:137)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.ActivityThread.main(ActivityThread.java:4745)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at java.lang.reflect.Method.invokeNative(Native Method)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at java.lang.reflect.Method.invoke(Method.java:511)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at dalvik.system.NativeStart.main(Native Method)
01-02 17:56:47.609: E/AndroidRuntime(1322): Caused by: java.lang.NullPointerException
01-02 17:56:47.609: E/AndroidRuntime(1322):     at com.example.test.DatabaseHelper.insert(DatabaseHelper.java:91)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at com.example.test.Results.showResults(Results.java:104)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at com.example.test.Results.onCreate(Results.java:50)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.Activity.performCreate(Activity.java:5008)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
01-02 17:56:47.609: E/AndroidRuntime(1322):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
01-02 17:56:47.609: E/AndroidRuntime(1322):     ... 11 more

EDIT: The NullPointerException is at least jumping around but errors still are there. I have edited my code and posted the new LogCat output.

8
  • Are you initializing DatabaseHelper dh; somewhere? Commented Jan 2, 2013 at 22:32
  • 3
    As the exception clearly states, the problem is at Results.showResults which I don't see anywhere in your post Commented Jan 2, 2013 at 22:33
  • I bet you forgot to call the open() method on your database Commented Jan 2, 2013 at 22:35
  • @K-ballo the dh.insert() call is in the showResults() method in the class Results. Sorry for the confusion. I should have shown that in my code posted. Commented Jan 2, 2013 at 22:57
  • @StefandeBruijn I never initialized DatabaseHelper dh anywhere. Could that be a problem? Commented Jan 2, 2013 at 22:58

4 Answers 4

1

Change openDB() to save the SqliteDatabase it opens and open a writable database:

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

But this still doesn't match your stack trace, however it matches the code you provided.

Sign up to request clarification or add additional context in comments.

3 Comments

I fixed what you suggested and the NPE went away. A NPE still exists just at a different spot now. I updated my opening post if you do not mind taking another look at it.
You are still using return this.getWritableDatabase(); change it too db = this.getWritableDatabase();.
Oh whoops, I thought I had it but apparently didn't. It works now! I now need to work on pulling back out what I inserted and displaying it to TextViews. Thanks again Sam.
1

You said you are doing:

DatabaseHelper dh;

dh.insert(1231423, 436346);

Looks like you are not instantiating dh:

dh = new DatabaseHelper(this);

A better approach would be to use the singleton pattern as you are ever going to need just one DatabaseHelper instance:

private static final String TABLE_NAME = "table_name";
private static final int SCHEME_VERSION = 1;
public static DatabaseHelper mSingleton = null;

private DatabaseHelper(Context ctxt) {
    super(ctxt, DATABASE_NAME, null, SCHEME_VERSION);
}

public synchronized static DatabaseHelper getInstance(Context ctxt) {
    if (mSingleton == null)
        mSingleton = new DatabaseHelper(ctxt.getApplicationContext());
    return mSingleton;
}

Now you can call like so:

DatabaseHelper dh = DatabaseHelper.getInstance(this);

1 Comment

The NPE is jumping around after implementing everything you had above. I edited my opening post and the new code, errors and LogCat.
0

If you are following the common SqlliteHelper pattern, you are likely to have forgotten to call the .open() method before calling insert.

4 Comments

You are right, I did not call open(). Should that be added in as the first line of code in the insert() method?
A way to use it is to call open() in onResume() method and close() in onPause() methods of your activity.
I updated what @StefandeBruijn and you suggested and edited my opening post with errors commented into the code. I made the code a little more clearer too.
I think you should init db in your public SQLiteDatabase openDB() { return this.getReadableDatabase(); } , something like db = this.getReadableDatabase()
-1
package com.example.hostelmangement;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper {

public DataBaseHelper(Context context, String name, CursorFactory factory,
        int version)
{
    super(context, name, factory, version);
    // TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db)
{
    db.execSQL("create table demo(id integer primary key,join_date date,name text,father_name text,date_of_birth text,house_name text,place text,pin_no text,blood_group text,mobile text,email text,company text,company_place text,company_phone text,company_pin,rent text,pending integer,status text,photo text,Proof text)");

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) 
{
    // TODO Auto-generated method stub

}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.