-1

I'm getting "Attempt to invoke virtual method on a null object reference" error.

The same code I had used previously and it worked perfectly. But don't know what's causing the error right now. I've tried a lot to solve this null pointer exception error but in vain.

Any help is appreciated. Thanks in advance.

'databseHelper.java'

public  Boolean checkEmail(String email)
{
    SQLiteDatabase db=this.getReadableDatabase();
    Cursor cursor=db.rawQuery("Select * from user where email=?",new String[]{email});
    if(cursor.getCount()>0)
        return true;
    else
        return  false;
}

'login.java'

Boolean email=db.checkEmail(user_email_id);
if(email==true)
{
    .....
}

The error I get is:

java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.example.tictactoe_new.databaseHelper.checkEmail(java.lang.String)' on a null object reference
Umang
  • 109
  • 2
  • 10
  • Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – forpas Sep 28 '19 at 18:24

1 Answers1

0

You call db.checkEmail(user_email_id). It seems that this db object is null.

You can fix it in two ways. Either make checkEmail static, so that you can call it on the type name:

Boolean email = databaseHelper.checkEmail(user_email_id);

or first create such an object:

 databaseHelper db = new databaseHelper();
 Boolean email = db.checkEmail(user_email_id);
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188