我正在处理字典数据库,我在第一次启动活动时插入查询,它运行顺利,并且在下次启动时我想阻止第二次插入,我使用这种方法通过检查是否row 是否为空,但它总是在我的 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
我将不胜感激。谢谢