我有一个SQLiteDatabaseOpenHelper
被调用的子类,DatabaseSuper
它覆盖onCreate
和onUpdate
方法。 DatabaseController
class 是其子类DatabaseSuper
,旨在实现所有应用程序特定的数据库操作。
我的问题是,当我getReadableDatabase()
在课堂上调用一个方法时DatabaseController
,它会抛出一个Nullpointer exceptio
n。但是,如果我将一个SQLiteDatabase
对象作为参数传递给从基类获取的方法(DatabaseSuper)
,则不会抛出异常。
方法
public String sendDate(SQLiteDatabase db) {
//SQLiteDatabase db = getReadableDatabase(); *line 1*
Cursor cursor = db.rawQuery("select date from todo", null);
StringBuffer stringBuffer = new StringBuffer();
while (cursor.moveToNext()) {
String date = cursor.getString(0);
stringBuffer.append(date + ":");
}
cursor.close();
return stringBuffer.toString();
}
从活动类调用方法
String data = dataControllerObject.sendDate(new DatabaseSuper(this).getReadableDatabase());
错误
06-18 04:28:50.239: E/AndroidRuntime(5147):
FATAL EXCEPTION: main
06-18 04:28:50.239:
E/AndroidRuntime(5147):
java.lang.RuntimeException:Unable to start activity ComponentInfo{com.example.dolist/com.example.dolist.SearchListView}:
java.lang.NullPointerException
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
06-18 04:28:50.239:
E/AndroidRuntime(5147):
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.os.Looper.loop(Looper.java:137)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.app.ActivityThread.main(ActivityThread.java:5041)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at java.lang.reflect.Method.invokeNative(Native Method)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at java.lang.reflect.Method.invoke(Method.java:511)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at dalvik.system.NativeStart.main(Native Method)
06-18 04:28:50.239: E/AndroidRuntime(5147):
Caused by: java.lang.NullPointerException
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at com.example.dolist.DataController.sendDate(DataController.java:37)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at com.example.dolist.SearchListView.onCreate(SearchListView.java:34)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.app.Activity.performCreate(Activity.java:5104)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-18 04:28:50.239: E/AndroidRuntime(5147):
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
06-18 04:28:50.239: E/AndroidRuntime(5147):
... 11 more