android sqlite 空指针错误
我正在开发一个需要数据库的android项目。我遇到的问题是当我启动它时,出现空指针异常错误。通过 logcat,我已经将范围缩小到数据库更新并尝试打开的时间。这是正常现象还是我看得太少了?
logcat:
02-12 15:41:33.810: ERROR/AndroidRuntime(354): java.lang.RuntimeException: Unable to start activity ComponentInfo{arch.field/arch.field.ChooseSite}: java.lang.NullPointerException
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.os.Looper.loop(Looper.java:123)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at java.lang.reflect.Method.invoke(Method.java:507)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at dalvik.system.NativeStart.main(Native Method)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): Caused by: java.lang.NullPointerException
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at arch.field.BmDb$DatabaseHelper.onUpgrade(BmDb.java:96)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:132)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at arch.field.BmDb.open(BmDb.java:110)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at arch.field.ChooseSite.onCreate(ChooseSite.java:30)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): ... 11 more
OnUpgrade 代码:
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2)
{
Log.d("MonkeyDatabase","DbUpdater");
db.execSQL("Drop table if exists "+TABLE_SITE_NAME);
db.execSQL("Drop table if exists "+TABLE_UNIT_NAME);
db.execSQL("Drop table if exists "+TABLE_BONESHELL_NAME);
db.execSQL("Drop table if exists "+TABLE_CERAMIC_NAME);
db.execSQL("Drop table if exists "+TABLE_LITHIC_NAME);
onCreate(db);
}
开放代码:
public BmDb open() throws SQLException
{
Log.d("Database","open");
db= BmDb.getWritableDatabase();
return this;
}
更多代码:
public BmDb(Context context)
{
this.context= context;
BmDb= new DatabaseHelper(context);
}
public static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, VERSION);
}
这是 ChooseSite 文件中的代码(带行号):
final BmDb db=new BmDb(this);
db.open(); **this is the line mentioned in my logcat**
final Cursor c= db.GetSite();
if (c.moveToFirst()){
do{
sites(c);
}while (c.moveToNext());
}
对我正在查看的内容有什么建议吗?
I am working on an android project that requires a database. The issue that I am having is when I go to start it up, i get a nullpointerexception error. Going through the logcat, I have narrowed it down, to when the database updating, and trying to open. Is this normal or am I over looking somthing?
logcat:
02-12 15:41:33.810: ERROR/AndroidRuntime(354): java.lang.RuntimeException: Unable to start activity ComponentInfo{arch.field/arch.field.ChooseSite}: java.lang.NullPointerException
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.os.Handler.dispatchMessage(Handler.java:99)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.os.Looper.loop(Looper.java:123)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.ActivityThread.main(ActivityThread.java:3683)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at java.lang.reflect.Method.invoke(Method.java:507)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at dalvik.system.NativeStart.main(Native Method)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): Caused by: java.lang.NullPointerException
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at arch.field.BmDb$DatabaseHelper.onUpgrade(BmDb.java:96)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:132)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at arch.field.BmDb.open(BmDb.java:110)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at arch.field.ChooseSite.onCreate(ChooseSite.java:30)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
02-12 15:41:33.810: ERROR/AndroidRuntime(354): ... 11 more
OnUpgrade code:
public void onUpgrade(SQLiteDatabase arg0, int arg1, int arg2)
{
Log.d("MonkeyDatabase","DbUpdater");
db.execSQL("Drop table if exists "+TABLE_SITE_NAME);
db.execSQL("Drop table if exists "+TABLE_UNIT_NAME);
db.execSQL("Drop table if exists "+TABLE_BONESHELL_NAME);
db.execSQL("Drop table if exists "+TABLE_CERAMIC_NAME);
db.execSQL("Drop table if exists "+TABLE_LITHIC_NAME);
onCreate(db);
}
Open code:
public BmDb open() throws SQLException
{
Log.d("Database","open");
db= BmDb.getWritableDatabase();
return this;
}
more code:
public BmDb(Context context)
{
this.context= context;
BmDb= new DatabaseHelper(context);
}
public static class DatabaseHelper extends SQLiteOpenHelper
{
DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, VERSION);
}
this is the code from the ChooseSite file(with line numbers):
final BmDb db=new BmDb(this);
db.open(); **this is the line mentioned in my logcat**
final Cursor c= db.GetSite();
if (c.moveToFirst()){
do{
sites(c);
}while (c.moveToNext());
}
any suggestions on what I am over looking?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
从此代码中
调用此方法两次 db.open(); 将其置于 try{}catch{} 黑色中。
如下 try{db.open();db.open();}catch(){}
from this code
call this mehod twice db.open(); put this in try{}catch{} black.
as follows try{db.open();db.open();}catch(){}
如果应用程序代码中的数据库版本增加,则在
onUpgrade
中调用
onUpgrade()
。检查你的代码..In your
onUpgrade
onUpgrade()
is called, if the database version is increased in your application code. check your code ..