1

Getting a null pointer exception at squlite query.

Following is my code snippet

String myPath = DB_PATH + DATABASE_NAME;
    SQLiteDatabase checkDB = null;     
    checkDB = SQLiteDatabase.openDatabase(myPath, null,SQLiteDatabase.OPEN_READONLY);
    if(checkDB != null){
    Cursor  cursor = database.rawQuery("SELECT Id, UserName FROM Login Where status="+ 1, null);
        if(cursor != null ) {
            if  (cursor.moveToFirst()){
                String UserName = cursor.getString(cursor.getColumnIndex("UserName"));
                String mail_id = cursor.getString(cursor.getColumnIndex("status"));
                Intent intent = new Intent(MainActivity.this, UserAccount.class);

                /*Sending some arguments*/ 
              Bundle bundle = new Bundle();

                  bundle.putString("UserName",UserName);
                  bundle.putString("Id", mail_id);
                  intent.putExtras(bundle);
                  startActivity(intent);

When I debugged the code, I have got a path value in checkDB variable but cursor value is null. So that I have getting null pointer exception at the line

        Cursor  cursor = database.rawQuery("SELECT Id, UserName FROM Login Where status="+ 1, null);

I couldn't find the source of reason, Please help me...

1
  • If you change to Cursor cursor = database.rawQuery("SELECT Id, UserName FROM Login Where status=1", null); does it works? Commented Jan 30, 2014 at 1:10

1 Answer 1

1

Where is the "database" variable initialized, because seems like what you should be using is "checkDB", maybe database variable is null by the time you are doing the rawQuery, didn't you want to go for checkDB.rawQuery(... instead?

Regards!

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

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.