0

i am searching for a number in my database and weather it exists in it or not and return a boolean but the query always gives me a null pointer exception when it is run

query

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

implementation

        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
        }

it always fails at the query

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): at android.app.ActivityThread.handleReceiver(ActivityThread.java:1794)

1
  • Where is line 87 in ContactDB.java? Commented Mar 16, 2011 at 16:59

3 Answers 3

2

You must open the database:

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

or it will throw NPEs. Don't forget to close it once you don't need it anymore.

Sign up to request clarification or add additional context in comments.

1 Comment

but the method getPhone is in the database class so it should not need to be opened right? *edit oh nevermind i just noticed i didnt open it when i called the getPhone method
0

Try changing up your db.query( ... ) line:

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

As per the documentation, your selection argument should not contain the WHERE keyword.

1 Comment

here where is a String which is passed as a parameter in the following method getPhone(String where).
0

the db needs to be opened. Also try executing the sql query using the rawquery(query,selectionargs) method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.