查询数据库时为空指针

时间:2011-03-16 16:55:28

标签: android nullpointerexception

我正在我的数据库中搜索一个数字并且天气存在于它中并返回一个布尔值,但查询总是在运行时给我一个空指针异常

查询

public boolean getPhone(String where){
    Cursor cur = db.query(DATABASE_TABLE, new String [] {ID,PHONE_NUMBER},PHONE_NUMBER + "='" + where + "'",null,null,null,null); //fails here at the query
        Log.v("Cursor", cur.toString());
        String test = null;
        if(cur.moveToFirst()){
            do{
                test = cur.getString(cur.getColumnIndex(PHONE_NUMBER));
                Log.v("ContactDB", test);
            }while(cur.moveToNext());
            return true;
        }
            return false;
    }

实施

        ContactDB db = new ContactDB(arg0);
        boolean dbNumber = db.getPhone(from);//searchNumber(arg0,from);
        if(dbNumber == true){
             //if number exists do stuff
        }else{
             //if number does not exist do other stuff
        }

它始终在查询中失败

03-16 11:57:06.522: ERROR/AndroidRuntime(28579): FATAL EXCEPTION: main
03-16 11:57:06.522: ERROR/AndroidRuntime(28579): java.lang.RuntimeException: Unable to start receiver com.app.notifyme.SmsReciever: java.lang.NullPointerException
03-16 11:57:06.522: ERROR/AndroidRuntime(28579):     at android.app.ActivityThread.handleReceiver(ActivityThread.java:1805)
03-16 11:57:06.522: ERROR/AndroidRuntime(28579):     at android.app.ActivityThread.access$2400(ActivityThread.java:117)
03-16 11:57:06.522: ERROR/AndroidRuntime(28579):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:981)
03-16 11:57:06.522: ERROR/AndroidRuntime(28579):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-16 11:57:06.522: ERROR/AndroidRuntime(28579):     at android.os.Looper.loop(Looper.java:123)
03-16 11:57:06.522: ERROR/AndroidRuntime(28579):     at android.app.ActivityThread.main(ActivityThread.java:3683)
03-16 11:57:06.522: ERROR/AndroidRuntime(28579):     at java.lang.reflect.Method.invokeNative(Native Method)
03-16 11:57:06.522: ERROR/AndroidRuntime(28579):     at java.lang.reflect.Method.invoke(Method.java:507)
03-16 11:57:06.522: ERROR/AndroidRuntime(28579):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-16 11:57:06.522: ERROR/AndroidRuntime(28579):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-16 11:57:06.522: ERROR/AndroidRuntime(28579):     at dalvik.system.NativeStart.main(Native Method)
03-16 11:57:06.522: ERROR/AndroidRuntime(28579): Caused by: java.lang.NullPointerException
03-16 11:57:06.522: ERROR/AndroidRuntime(28579):     at com.app.notifyme.ContactDB.getPhone(ContactDB.java:87)
03-16 11:57:06.522: ERROR/AndroidRuntime(28579):     at com.app.notifyme.SmsReciever.onReceive(SmsReciever.java:55)

03-16 11:57:06.522:ERROR / AndroidRuntime(28579):在android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)

3 个答案:

答案 0 :(得分:2)

您必须打开数据库:

ContactDB db = new ContactDB(arg0);
db.open();

或者它会抛出NPE。一旦你不再需要它,别忘了close

答案 1 :(得分:0)

尝试更改db.query( ... )行:

Cursor cur = db.query(
  DATABASE_TABLE,
  new String [] {ID,PHONE_NUMBER},
  PHONE_NUMBER + " = " + theNumber,
  null, null, null, null, null);

根据the documentation,您的selection参数不应包含WHERE关键字。

答案 2 :(得分:0)

需要打开数据库。还尝试使用rawquery(query,selectionargs)方法执行sql查询。