openorcreatedatabase给出nullpointerexception

时间:2014-02-23 14:45:38

标签: android sqlite nullpointerexception

我之前在我正在使用的Android应用程序中使用过数据库 所以我通常打开()数据库 当我的查询完成时,我再次关闭()它

对我的数据库类的最新更改我收到错误 给出以下痕迹

  02-23 14:27:55.261: I/webserver do in background(23728): bezoek = [Ljava.lang.String;@40e324a0
02-23 14:27:55.261: D/AndroidRuntime(23728): Shutting down VM
02-23 14:27:55.261: W/dalvikvm(23728): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
02-23 14:27:55.311: E/AndroidRuntime(23728): FATAL EXCEPTION: main
02-23 14:27:55.311: E/AndroidRuntime(23728): java.lang.NullPointerException
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:229)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at com.example.deceunincktechniekers.ABezoeAdapter$dbhulp.<init>(ABezoeAdapter.java:60)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at com.example.deceunincktechniekers.ABezoeAdapter.open(ABezoeAdapter.java:87)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at com.example.deceunincktechniekers.AWebServAdapter.synchroniseren(AWebServAdapter.java:186)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at com.example.deceunincktechniekers.bezoekrapporten.handlescan(bezoekrapporten.java:146)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at com.example.deceunincktechniekers.bezoekrapporten$1.onEditorAction(bezoekrapporten.java:68)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.widget.TextView.doKeyDown(TextView.java:5314)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.widget.TextView.onKeyDown(TextView.java:5204)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.view.KeyEvent.dispatch(KeyEvent.java:2609)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.view.View.dispatchKeyEvent(View.java:7205)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.widget.ScrollView.dispatchKeyEvent(ScrollView.java:353)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1920)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1395)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.app.Activity.dispatchKeyEvent(Activity.java:2370)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1847)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.os.Looper.loop(Looper.java:137)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at android.app.ActivityThread.main(ActivityThread.java:5041)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at java.lang.reflect.Method.invokeNative(Native Method)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at java.lang.reflect.Method.invoke(Method.java:511)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-23 14:27:55.311: E/AndroidRuntime(23728):    at dalvik.system.NativeStart.main(Native Method)

这个错误发生了,因为我已经改变了这部分代码

public void gegevensupdaten(String bezoeknummer, String[] data) {
            Log.i("AbezoeAdapter gegevensupdaten", "bezoeknummer= " + bezoeknummer + "gegevens = " + data);
            open();
            ContentValues cv = new ContentValues();

            cv.put(bezoekrapportdatum, data[2]);
            cv.put(herstellingsoort, data[3]);


            Log.i("test","gelukt");


            onzedatabase.update(databasetabel, cv,  bezoekrapportnummer + "=?", new String[] {bezoeknummer});
            sluit();
            return;
        }

在调用函数open()时发生错误 但是这个函数是开放的,因为它总是在我到达这部分代码之前使用(并且在那一刻它不会阻塞)

我的open()方法如下:

private static class dbhulp extends SQLiteOpenHelper
{

    dbhulp(Context context) {
        super(context, ADbAdapter.databasenaam, null, ADbAdapter.databaseversie);
        getWritableDatabase();
        Log.i("abezoe dbhulp", "dbhulp goed doorbracht");
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        ADbAdapter.onCreate(db);
        Log.i("adbadapter", "oncreate opgeroepen");

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        ADbAdapter.onUpgrade(db, oldVersion, newVersion);
        Log.i("adbadapter", "onupgrade opgeroepen");


    }



}
public ABezoeAdapter(Context c){
    onzecontext = c;
}

public ABezoeAdapter open() throws SQLException{
    onzehelper = new dbhulp(onzecontext);
    onzedatabase = onzehelper.getWritableDatabase();
    Log.i("abezoeadapter open", "gelukt");
    return this;
}

public void sluit(){
    onzehelper.close();

}
在互联网上搜索数小时了解到我并不是唯一一个反对这个错误的人,但似乎没有人为我的问题提供良好的解决方案 感谢您的帮助

修改 我刚发现post可能会解决同样的问题

但我不明白给出的答案:

  

“这就是原因。接受的答案很混乱。问题   从问题的堆栈跟踪中可以看出:数据库正在打开   MyApp的。即成员变量初始化,很久以前   的onCreate()。 - laalto '13年12月29日7:31“

任何人都可以帮助理解我的失败吗?

2 个答案:

答案 0 :(得分:1)

堆栈跟踪表示您传递给Context的{​​{1}}为SQLiteOpenHelper

该代码未显示您如何初始化null来自ABezoeAdapter的地方,但这是您应该寻求解决问题的地方。

答案 1 :(得分:0)

我刚刚解决了我的问题!! (快乐的方式)

我从我的第3类AWebServerAdapter调用了函数gegevensupdaten()

该类的上下文似乎有问题,可以与我的第二个(Abezoeadapter)类进行databaseactions

所以我将数据从网络服务器返回到我的主要(第一,bezoekrapporten)课程,并从我的第一堂课中调用gegevensupdaten()

为什么这个3级课程不起作用对我来说仍然是一个谜。