-2

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

我将不胜感激。谢谢

4

2 回答 2

0

看起来您的数据库变量为,这就是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
    }
}
于 2013-03-14T23:48:40.440 回答
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;
}
于 2013-03-14T23:57:04.283 回答