-3

I am trying to set the log in system on android using a username check between what the user inputs and what exists in the DB

Here is my piece of code, but it always gives me "invalid" and if i take the Try Catch block, it gives me a nullpointerException

public void checklogin(String logged){
    SQLiteDatabase db = null;
    DatabaseAdapter database = new DatabaseAdapter(this);       

        try{               
    Cursor mCursor = db.query(database.TABLE_USERS,new String[]{"select * from users"}, " Name like" + "'logged+'",null,null,null,null);

     if  (mCursor.moveToFirst()){
         Toast.makeText(LoginActivity.this, "Yaaaay", Toast.LENGTH_LONG).show();
     }}
        catch(NullPointerException e){
      Toast.makeText(LoginActivity.this, "invalid", Toast.LENGTH_LONG).show();

        }
        }
Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
callback
  • 3,981
  • 1
  • 31
  • 55

1 Answers1

1

Well, you set db to be null... so when you call db.query it should rightfully give you a NullPointerException.

Alex Lockwood
  • 83,063
  • 39
  • 206
  • 250
  • I have corrected that by defining db in the beginning of the class.. but it still gives me "invalid" even if i enter a valid name. Is there any problem with the syntax of the cursor? – callback Apr 22 '12 at 22:41
  • You define `db` to be a local variable at the top of the method when you set `SQLiteDatabase db = null;`, so that is the reason why you are getting a `NullPointerException`. – Alex Lockwood Apr 22 '12 at 22:43
  • but yeah, there is also an issue with your syntax. see my edited post. – Alex Lockwood Apr 22 '12 at 22:45
  • Actually, just check this [link](http://stackoverflow.com/questions/1243199/sqlite-query-in-android-application). – Alex Lockwood Apr 22 '12 at 22:49