检查表行是否为空

时间:2013-03-14 23:43:39

标签: android database sqlite android-sqlite

我正在处理字典数据库,我在活动首次启动时插入查询,它工作顺利,并且在下次启动时我想阻止第二次插入,我使用此方法来防止插入检查行是否为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

我将不胜感激任何帮助。感谢

2 个答案:

答案 0 :(得分:0)

您的数据库变量看起来像 null ,这是 NPE 的原因。因此,如果为null,则需要确保变量将被初始化。

所以这里是条件如何实现它的方法:

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
    }
}

答案 1 :(得分: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;
}