1

I am trying to copy a database out of assets but in my createDatabase() method I am getting a null pointer exception. This occurs on the call to getReadableDatabase.

public static synchronized MetroLinkDatabaseHelper getInstance(Context context) {

//        Using a singleton to minmize the chance of opening multiple
//        decreasing any chance of memory leak
        if(myInstance == null) {
            myInstance = new MetroLinkDatabaseHelper(context);
        }
        return myInstance;
    }
  private MetroLinkDatabaseHelper(Context context) {
        super(context, DB_NAME, null, 1);
        this.mycontext = context;
        boolean dbexist = checkdatabase();
        if (dbexist) {
        } else {
            System.out.println("Database doesn't exist");
            try {
                createdatabase();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    //      Create (copy the original in assets) if not exist using copydatabase
    public void createdatabase() throws IOException {
        boolean dbexist = checkdatabase();
        if (dbexist) {
        } else {
//            SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
             this.getReadableDatabase();
                 try {

                copydatabase();
            } catch (IOException e) {
                throw new Error("Error copying database");
            }
        }
    }

Here is the error log:

08-29 14:13:05.147    2222-2222/com.bkane56.practice.practiceapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.bkane56.practice.practiceapp, PID: 2222
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.bkane56.practice.practiceapp/com.bkane56.practice.practiceapp.ListStopsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2250)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
            at android.app.ActivityThread.access$800(ActivityThread.java:155)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
            at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:268)
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
            at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:187)
            at com.bkane56.practice.practiceapp.MetroLinkDatabaseHelper.createdatabase(MetroLinkDatabaseHelper.java:66)
            at com.bkane56.practice.practiceapp.MetroLinkDatabaseHelper.<init>(MetroLinkDatabaseHelper.java:52)
            at com.bkane56.practice.practiceapp.MetroLinkDatabaseHelper.getInstance(MetroLinkDatabaseHelper.java:38)
            at com.bkane56.practice.practiceapp.ListStopsActivity.<init>(ListStopsActivity.java:28)
            at java.lang.reflect.Constructor.newInstance(Native Method)
            at java.lang.Class.newInstance(Class.java:1606)
            at android.app.Instrumentation.newActivity(Instrumentation.java:1089)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2240)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
            at android.app.ActivityThread.access$800(ActivityThread.java:155)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

Line 66 =

this.getReadableDatabase();

Any ideas? I did ask this question before in a much longer form but got no help.

Thanks

1 Answer 1

1

Your problem is here:

com.bkane56.practice.practiceapp.ListStopsActivity.<init>(ListStopsActivity.java:28)

You are attempting to do something with the Activity instance before the super.onCreate() call in the onCreate() method of that Activity. Generally, that does not work, with exceptions like this one.

Instead of calling getInstance() on MetroLinkDatabaseHelper from an initializer, move that call to after super.onCreate() in your onCreate() method.

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

1 Comment

@CommonsWare....Thank you so much...I've been effing around with this for a full day. I did have to declare it as class variable and then make the call after the super.onCreate. Thanks again.

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.