开发者

Null Pointer Creating SQLite Database

开发者 https://www.devze.com 2023-03-29 20:56 出处:网络
Why do these two lines of code throw a NPE? SQLiteDatabase db; db = openOrCreateDatabase(\"TestingData.db\", SQLiteDatabase.CREATE_IF_NECESSARY, null);

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)


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消