0

My application is crashing due to this query. what should i do to know whats the problem inside? It seems correct to me

public User getUser(String userName) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = null;

    cursor = db.query("LOGIN", new String[] { "USERNAME",
            "PASSWORD", "STATUS" }, "USERNAME" + "=?",
            new String[] { String.valueOf(userName) }, null, null, null, null);
    if (cursor != null)
    {
    cursor.moveToFirst();
    }
    User user = new User();
    user.setName(cursor.getString(cursor.getColumnIndex("USERNAME")));
    user.setPassword(cursor.getString(cursor.getColumnIndex("PASSWORD")));
    user.setstatus(cursor.getString(cursor.getColumnIndex("STATUS")));

    return user;

}
hira
  • 29
  • 5

2 Answers2

1

Probably your cursor is returning an Empty/NULL cursor... Now you have put a check on it as

 if (cursor != null)
 {
 cursor.moveToFirst();
 }

But after this you are directly trying to set your user data from the cursor without checking if it is null, Try this instead:

User user=null;
if (cursor != null)
{
user= new User();
cursor.moveToFirst();
user.setName(cursor.getString(cursor.getColumnIndex("USERNAME")));
user.setPassword(cursor.getString(cursor.getColumnIndex("PASSWORD")));
user.setstatus(cursor.getString(cursor.getColumnIndex("STATUS")));
}

if(user==null){
    // your cursor returned null... check your query/db 
}

//this will return a null object if your cursor returns null... you can handle it later
return user;
Kushan
  • 5,855
  • 3
  • 31
  • 45
0

you can check query using SQliteManger or the following link open and write your query then you will find its correct or not

https://www.piliapp.com/mysql-syntax-check/

Dinesh Saini
  • 335
  • 1
  • 12