2013-03-14 65 views
-2

我正在處理字典數據庫,當活動第一次啓動時插入查詢,它工作正常,並且在下次啓動時我要阻止第二次插入,我使用這種方法通過檢查行是否爲null來防止插入,但是它總是在我的logcat中產生一個java空指針異常。檢查錶行是否爲空

public boolean isEmpty() 
{ 
    boolean empty = false; 
    Cursor cursor = database.rawQuery("SELECT COUNT(*) FROM " + SQLiteHelper.DATABASE_TABLE, null); 

    if(cursor != null) 
    { 
     cursor.moveToFirst(); 
     if(cursor.getInt(0)== 0) 
     { 
      empty = false; //rows not null; 
     } 
     else 
     { 
      empty = true; // rows null; 
     } 
    } 
    return empty; 
} 

這是將在每次啓動

if(datasource.isEmpty() == false) 
    { 
     try 
     { 
      datasource.opentowrite(); 
      datasource.Insert("aback", "tertegun"); 
      datasource.Insert("abacus", "dekak-dekak"); 
      datasource.Insert("abandon", "meninggalkan"); 
      datasource.Insert("abase", "menghinakan"); 
      datasource.Insert("abesement", "penghinaan"); 
      datasource.Insert("abash", "memberi malu"); 
      datasource.Insert("abate", "mengurangkan"); 
      datasource.close(); 
     } 
     catch(Exception ex) 
     { 
      Toast.makeText(getApplicationContext(), "Database faulty", Toast.LENGTH_LONG).show(); 
     } 
    } 
    else 
    { 
     Toast.makeText(getApplicationContext(), "Database ready", Toast.LENGTH_LONG).show(); 
    } 

執行的主要活動我插入的代碼,這是logcat的

03-15 06:41:06.547: E/AndroidRuntime(455): FATAL EXCEPTION: main 
03-15 06:41:06.547: E/AndroidRuntime(455): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dictionary/com.dictionary.Dictionary}: java.lang.NullPointerException 
03-15 06:41:06.547: E/AndroidRuntime(455): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
03-15 06:41:06.547: E/AndroidRuntime(455): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
03-15 06:41:06.547: E/AndroidRuntime(455): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
03-15 06:41:06.547: E/AndroidRuntime(455): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
03-15 06:41:06.547: E/AndroidRuntime(455): at android.os.Handler.dispatchMessage(Handler.java:99) 
03-15 06:41:06.547: E/AndroidRuntime(455): at android.os.Looper.loop(Looper.java:123) 
03-15 06:41:06.547: E/AndroidRuntime(455): at android.app.ActivityThread.main(ActivityThread.java:4627) 
03-15 06:41:06.547: E/AndroidRuntime(455): at java.lang.reflect.Method.invokeNative(Native Method) 
03-15 06:41:06.547: E/AndroidRuntime(455): at java.lang.reflect.Method.invoke(Method.java:521) 
03-15 06:41:06.547: E/AndroidRuntime(455): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
03-15 06:41:06.547: E/AndroidRuntime(455): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
03-15 06:41:06.547: E/AndroidRuntime(455): at dalvik.system.NativeStart.main(Native Method) 
03-15 06:41:06.547: E/AndroidRuntime(455): Caused by: java.lang.NullPointerException 
03-15 06:41:06.547: E/AndroidRuntime(455): at com.dictionary.KamusDataSource.isEmpty(KamusDataSource.java:36) 
03-15 06:41:06.547: E/AndroidRuntime(455): at com.dictionary.Dictionary.onCreate(Dictionary.java:29) 
03-15 06:41:06.547: E/AndroidRuntime(455): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
03-15 06:41:06.547: E/AndroidRuntime(455): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
03-15 06:41:06.547: E/AndroidRuntime(455): ... 11 more 

我會明白任何幫助。謝謝

回答

0

它看起來像你的數據庫變量是null這就是爲什麼NPE。所以你需要確保該變量將被初始化爲空。

因此,這裏是法條件如何實現它:

public void methodName() { 
    if (database == null) { // this will ensure that database will be initialised 
     // if is null 
     database = dbHelper.getReadableDatabase(); // or similar method that 
     // that returns new SQLiteDatabase instance 
    } 
    Cursor c = database.rawQuery("SELECT COUNT(*) FROM TableName", null); 
    if (cursor != null) { 
     // do your stuff 
    } 
} 
+0

對不起,你可以給數據庫的初始化的例子嗎? – 2013-03-14 23:57:11

+1

所以你創建了一些dbhelper,對吧?所以做一些像database = dbhelper.getWriteableDatabase()。 – Sajmon 2013-03-15 00:05:02

+0

是的,我已經做到了在這個類中,我有兩個方法,一個用於讀,另一個寫 '公共無效opentowrite()拋出的SQLException \t { \t \t數據庫= dbHelper.getWritableDatabase(); \t} \t \t 公共無效opentoread()拋出的SQLException \t { \t \t數據庫= dbHelper.getReadableDatabase(); \t}' – 2013-03-15 00:11:00

0

應該是這樣

public boolean isEmpty() 
{ 
    boolean empty = false; 
    Cursor cursor = database.rawQuery("SELECT COUNT(*) FROM " + SQLiteHelper.DATABASE_TABLE, null); 


    if(cursor.getCount > 0) 
    { 
     empty = false; //rows not null; 
    } 
    else 
    { 
     empty = true; // rows null; 
    } 
} 
return empty; 
} 
+0

謝謝,但它不起作用。 – 2013-03-15 00:02:47

+0

這行是否引發異常:if(datasource.isEmpty()== false) – 2013-03-15 00:04:11

+0

是的,它會拋出異常 – 2013-03-15 00:06:21