2012-02-13 60 views
0

我正在處理一個需要數據庫的android項目。我遇到的問題是當我啓動它時,出現nullpointerexception錯誤。通過logcat,我縮小了範圍,到了數據庫更新的時候,並試圖打開。這是正常的,還是我看起來很麻煩?
logcat的:android sqlite nullpointer錯誤

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

對我在看什麼有什麼建議嗎?

回答

3

從該代碼

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

呼叫這個mehod兩次db.open();把它放在try {} catch {}中。

如下嘗試{db.open(); db.open();}趕上(){}

+0

爲什麼這會幫助糾正它?它的工作,但剛剛經歷了另一個錯誤,那人會與我的GetSite代碼,這就是: 公共光標GetSite() \t { \t \t回db.query(TABLE_SITE_NAME,新的String [] {KEY_SITEID, KEY_SITENAME,KEY_SITELOC},null,null,null,null,null); \t} 我是使用SQLite與android的新手,但從我發現的所有教程中,這是常見的形式。我標記錯了嗎? – 2012-02-13 04:33:19

+0

看着android開發人員網站,我發現了遊標查詢的兩個實例。公共遊標查詢(SQLiteDatabase db,String [] projectionIn,String selection,String [] selectionArgs,String groupBy,String having, String sortOrder) 據我所知,我正確地遵循它。有什麼我缺少的東西嗎? – 2012-02-13 04:54:48

+0

好吧,我感覺很蠢。爲什麼try {} catch(){}語句使所有不同?這也解決了我剛剛評論過的其他問題。我還發現,我只需要在try {} catch {}中調用db.open()一次。 – 2012-02-13 05:11:06

0

在你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()被調用時,如果數據庫版本增加在您的應用程序代碼。檢查你的代碼..

+0

要升級的代碼,是在實際程序中標記的還是會在我的數據庫文件頂部;我稱之爲VERSION? – 2012-02-13 04:36:30