0

Why do these two lines of code throw a NPE?

SQLiteDatabase db;
db = openOrCreateDatabase("TestingData.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);

Stacktrace:

08-23 10:33:14.285: ERROR/AndroidRuntime(1001): Caused by: java.lang.NullPointerException
08-23 10:33:14.285: ERROR/AndroidRuntime(1001):     at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
08-23 10:33:14.285: ERROR/AndroidRuntime(1001):     at org.*****.android.CopyOfSQLLite.data(CopyOfSQLLite.java:14)
08-23 10:33:14.285: ERROR/AndroidRuntime(1001):     at org.******.android.*****.****(****.java:200)
08-23 10:33:14.285: ERROR/AndroidRuntime(1001):     at org.********.android.****.onCreate(DashboardPage.java:25)
08-23 10:33:14.285: ERROR/AndroidRuntime(1001):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-23 10:33:14.285: ERROR/AndroidRuntime(1001):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
2

2 Answers 2

1

Your ContextWrapper does not have a base Context. Most likely you are not calling this method at the correct place, but I cannot say more without a little more code.

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

Comments

0

The nice thing about Android is that it is open source. So we can see that openOrCreateDatabase() is implemented like:

public SQLiteDatabase openOrCreateDatabase(String name, int mode, CursorFactory factory) {
    return mBase.openOrCreateDatabase(name, mode, factory);
}

The only way I can see this code throwing a NPE is if mBase is null. From your calling convention I assume your class is derived from android.content.ContextWrapper? If so, are you ensuring that the base context (mBase) is set when your class is instantiated? If you aren't, then that would be what's causing the problem.

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.