openorcreatedatabase 给出空指针异常 [英] openorcreatedatabase gives nullpointerexception
问题描述
我之前在我正在开发的 android 应用程序中使用过数据库所以通常我打开()数据库当我的查询完成后,我再次关闭()
i used databases before in the android app i am working on so usualy i open() the database when my query is finished i close() it again
我对数据库类的最新更改出现错误给出以下痕迹
with my latest change to my database class i get an error giving following traces
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)
这个错误发生是因为我改变了这部分代码
this errors occure since i have changed this part of code
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()时发生错误但是这个函数是开放的,因为它总是在我到达这部分代码之前使用(并且在那一刻它不会阻塞)
the error occures at the moment of calling the function open() but this function is open as it is always used before i reach this part of code (and at that moment it doesn't block)
我的 open() 方法如下所示:
my open() method looks like:
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();
}
在互联网上搜索了几个小时,我发现我不是唯一一个遇到此错误的人,但似乎没有人为我的问题提供好的解决方案提前感谢您的帮助
hours of searching on the internet learned me that i am not the only one running against this error but nobody seems to provide a good sollution for my problem thanks for your help in advance
我刚刚发现了一个帖子,可能涉及相同的问题
i just found a post that probably deals about the same problem
但我不明白给出的答案:
but i don't understand the answer given:
这就是原因.接受的答案令人困惑.问题从问题的堆栈跟踪中可以明显看出:数据库正在打开我的应用程序.即成员变量初始化,早在onCreate().– 拉尔托 2013 年 12 月 29 日 7:31"
"This is the reason. The accepted answer is confounded. The problem is evident from the question's stacktrace: database being opened in MyApp. i.e. member variable initialization, well before onCreate(). – laalto Dec 29 '13 at 7:31"
有人可以帮助理解我的失败吗?
can anybody help to understand my failure?
推荐答案
堆栈跟踪表明您传递给 SQLiteOpenHelper
的 Context
是 null
.
The stacktrace says the Context
you passed to SQLiteOpenHelper
was null
.
代码没有显示您如何初始化 ABezoeAdapter
哪里是 Context
的来源,但这正是您应该寻求解决问题的地方.
The code doesn't show how you initialize your ABezoeAdapter
where the Context
comes from but that's where you should be looking to solve the problem.
这篇关于openorcreatedatabase 给出空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!