[英]Null Pointer Creating SQLite Database
Why do these two lines of code throw a NPE? 为什么这两行代码会引发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. 您的ContextWrapper没有基本的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 . Android的好处是它是开源的 。 So we can see that
openOrCreateDatabase()
is implemented like: 因此我们可以看到
openOrCreateDatabase()
的实现方式如下:
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. 我看到此代码
mBase
NPE的唯一方法是mBase
为null。 From your calling convention I assume your class is derived from android.content.ContextWrapper
? 根据您的调用约定,我假设您的类派生自
android.content.ContextWrapper
? If so, are you ensuring that the base context ( mBase
) is set when your class is instantiated? 如果是这样,您是否确保在实例化类时设置了基本上下文(
mBase
)? If you aren't, then that would be what's causing the problem. 如果不是,那就是导致问题的原因。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.