0

我有一个SQLiteDatabaseOpenHelper被调用的子类,DatabaseSuper它覆盖onCreateonUpdate方法。 DatabaseControllerclass 是其子类DatabaseSuper,旨在实现所有应用程序特定的数据库操作。

我的问题是,当我getReadableDatabase()在课堂上调用一个方法时DatabaseController,它会抛出一个Nullpointer exception。但是,如果我将一个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
4

1 回答 1

0

在构造函数中Context传递给的是.SQLiteOpenHelpernull

在调用部分

String data = dataControllerObject.sendDate(new DatabaseSuper().getReadableDatabase());

你实际上并没有传递任何Context-new DatabaseSuper()没有任何参数。超SQLiteOpenHelper类构造函数需要一个,因此您可能会传入一个显式的null. 如需详细帮助,请发布您的构造函数代码。

于 2015-06-18T05:42:04.957 回答