I am trying to check if a sqlite database is empty using
public boolean chkDB(){
boolean chk = false;
Cursor mCursor = db.rawQuery("SELECT * FROM " + DATABASE_TABLE, null);
if (mCursor != null){
mCursor.moveToFirst();
if (mCursor.getInt(0) == 0){
chk = false;
}
}else{
chk = true;
}
return chk;
}
but every time i call that method i get null pointer exception
My Logcat shows this
06-28 22:35:19.519: E/AndroidRuntime(441): at com.android.id.DBAdapter.chkDB(DBAdapter.java:82)
06-28 22:58:06.269: E/AndroidRuntime(621): FATAL EXCEPTION: main
06-28 22:58:06.269: E/AndroidRuntime(621): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.id/com.android.id.MainActivity}: java.lang.NullPointerException
06-28 22:58:06.269: E/AndroidRuntime(621): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-28 22:58:06.269: E/AndroidRuntime(621): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-28 22:58:06.269: E/AndroidRuntime(621): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-28 22:58:06.269: E/AndroidRuntime(621): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-28 22:58:06.269: E/AndroidRuntime(621): at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 22:58:06.269: E/AndroidRuntime(621): at android.os.Looper.loop(Looper.java:123)
06-28 22:58:06.269: E/AndroidRuntime(621): at android.app.ActivityThread.main(ActivityThread.java:4627)
06-28 22:58:06.269: E/AndroidRuntime(621): at java.lang.reflect.Method.invokeNative(Native Method)
06-28 22:58:06.269: E/AndroidRuntime(621): at java.lang.reflect.Method.invoke(Method.java:521)
06-28 22:58:06.269: E/AndroidRuntime(621): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-28 22:58:06.269: E/AndroidRuntime(621): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-28 22:58:06.269: E/AndroidRuntime(621): at dalvik.system.NativeStart.main(Native Method)
06-28 22:58:06.269: E/AndroidRuntime(621): Caused by: java.lang.NullPointerException
06-28 22:58:06.269: E/AndroidRuntime(621): at com.android.id.DBAdapter.chkDB(DBAdapter.java:82)
06-28 22:58:06.269: E/AndroidRuntime(621): at com.android.id.MainActivity.enterDB(MainActivity.java:66)
06-28 22:58:06.269: E/AndroidRuntime(621): at com.android.id.MainActivity.onCreate(MainActivity.java:23)
06-28 22:58:06.269: E/AndroidRuntime(621): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-28 22:58:06.269: E/AndroidRuntime(621): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-28 22:58:06.269: E/AndroidRuntime(621): ... 11 more
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…