android sqlite 空指针错误

发布于 2025-01-05 02:45:06 字数 3833 浏览 2 评论 0原文

我正在开发一个需要数据库的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小糖芽 2025-01-12 02:45:06

从此代码中

 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());
}

调用此方法两次 db.open(); 将其置于 try{}catch{} 黑色中。

如下 try{db.open();db.open();}catch(){}

from this code

 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());
}

call this mehod twice db.open(); put this in try{}catch{} black.

as follows try{db.open();db.open();}catch(){}

╭ゆ眷念 2025-01-12 02:45:06

如果应用程序代码中的数据库版本增加,则在 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);
         onCreate(arg0);
        }

调用 onUpgrade()。检查你的代码..

In your 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);
         onCreate(arg0);
        }

onUpgrade() is called, if the database version is increased in your application code. check your code ..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文