Robert
Robert

Reputation: 343

Getting null pointer exception at sqlite query

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

Upvotes: 1

Views: 452

Answers (1)

Martin Cazares
Martin Cazares

Reputation: 13705

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!

Upvotes: 1

Related Questions