-1

I'm having a problem at the moment with android studio as my checkLogIn method doesn't seem to work at all. I was wondering if someone could point out any noticeable flaws?

edit: When I call the method in the main activity I still get an error message. on the logcat it shows "getSlotFromBufferLocked: unknown buffer"

//Class manages all data pertaining to the login page

public class DBHandler extends SQLiteOpenHelper {

//Declare constants
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "logindetails.db";
private static final String TABLE_LOGIN = "Logindetails";
private static final String COLUMN_ID = "_id";
private static final String COLUMN_LOGIN = "_logIn";
private static final String COLUMN_PASS = "_password";

//Constructor
public DBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
}

//On create method - passes through a database object
public void onCreate(SQLiteDatabase db){
    //Declares column and table names and data types
    String query = "CREATE TABLE " + TABLE_LOGIN + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
            COLUMN_LOGIN + " TEXT," +
            COLUMN_PASS + " TEXT" +
            ");";
    db.execSQL(query);
}

//On upgrade method
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);
    onCreate(db);
}

//Adds login details to database - Takes a login object as a parameter.
public void addLogIn(LogIns log){
    //Declare values and select columns to pass values through
    SQLiteDatabase db = this.getWritableDatabase();
    ContentValues values = new ContentValues();

    values.put(COLUMN_LOGIN, log.get_logIn());
    values.put(COLUMN_PASS, log.get_password());

    //Insert values into the table
    db.insert(TABLE_LOGIN, null, values);
    db.close();
}

//reads database and gets list of logins - takes the username and password as an argument
public boolean checkLogIn(String user, String pass){
    //Declare and initialise variables
    boolean success = false;
    SQLiteDatabase db = this.getReadableDatabase();
    //select columns in a string array
    String[] columns = {COLUMN_LOGIN, COLUMN_PASS};

    //execute query
    Cursor c = db.query(TABLE_LOGIN, columns, null, null, null, null, null);

    //Temp string to store results from query
    String rName = "";
    String rPass = "";

    //loop through all rows and data
    for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
        int iName = c.getColumnIndex(COLUMN_LOGIN);
        int iPass = c.getColumnIndex(COLUMN_PASS);
        rName = c.getString(iName);
        rPass = c.getString(iPass);

        if (user.equals(rName) && pass.equals(rPass)){
            success = true;
            break;
        }
    }
    return success;

}

}

waterytart
  • 19
  • 6
  • Where is your SQLiteHelper object? – Daniel May 22 '16 at 16:27
  • do you actually need one? – waterytart May 22 '16 at 16:28
  • sorry, i'm sort of a noob. What do you mean by the logcat? – waterytart May 22 '16 at 16:33
  • http://www.javahotchocolate.com/tutorials/tut-images/android-logcat.png – Nenco May 22 '16 at 16:33
  • In most cases yes, it aids with database creation & management. https://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html, I am wondering how you have access to the `getReadableDatabase()` method without it. – Daniel May 22 '16 at 16:34
  • 05-22 17:41:21.922 2503-2623/blablablabla.councilapp E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f903dcaf0a0 05-22 17:41:27.975 2503-2623/blablablabla.councilapp E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7f903dcaed20 – waterytart May 22 '16 at 16:35
  • @DanielK public DBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version){ super(context, DATABASE_NAME, factory, DATABASE_VERSION); } – waterytart May 22 '16 at 16:37
  • make a screenshot of your logcat error and update your question with lines where you got the error – Nenco May 22 '16 at 16:37
  • @Selvin I think I do. I'm just having problems with SQL specifically. My lecturer just sort of sprung it on us without actually going through it. – waterytart May 22 '16 at 16:39
  • No you don't.. `rName == user` seriously? – Selvin May 22 '16 at 16:42
  • Possible duplicate of [How do I compare strings in Java?](http://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java) – Selvin May 22 '16 at 16:44
  • @Selvin Okay, my bad. No need to criticise – waterytart May 22 '16 at 16:45
  • @waterytart, programming is tough, detail oriented business. We all miss things, no big deal. Just keep at it. – Daniel May 22 '16 at 16:48

1 Answers1

1

Your string comparison is incorrect.

(rName == user & rPass == pass)

This compares references not values, you want:

(rName.equals(user) && rPass.equals(pass))

This compares actual string values.

Further reading:

How do I compare strings in Java? What is a NullPointerException, and how do I fix it?

If you have anymore issues, let me know in the comments.

Community
  • 1
  • 1
Daniel
  • 2,367
  • 9
  • 22
  • 30
  • unfortunately I still get the same error. getSlotFromBufferLocked: unknown buffer: 0x7ff0c63567a0 – waterytart May 22 '16 at 16:56
  • @waterytart, okay so go ahead and post the logcat when the error happens. So the logcat is the information at the bottom of your screen in Android Studio. – Daniel May 22 '16 at 17:00
  • can't post pics so i'll have to use this http://i63.tinypic.com/2llog3c.png – waterytart May 22 '16 at 17:02
  • Ah yes there you go. What we need to see is the part towards the top that says "Caused by java......NullPointerException". That's the error that needs to be fixed. I'll also update my answer with references to NPEs. – Daniel May 22 '16 at 17:04
  • http://i63.tinypic.com/xdc7kj.png this part? – waterytart May 22 '16 at 17:06
  • Yes that. So it says that the comparison you're making at `rName.equals(user)` is not valid because` rName` is `null`. Do you have values stored in the database you're pulling from? or are you sure you're sending in values for pass & user? – Daniel May 22 '16 at 17:12
  • well, before i try using that method i use the addLogIn method first. I'll edit the post so you can see the whole class – waterytart May 22 '16 at 17:14
  • @waterytart, so you're calling `dbhelper.addLogIn(LogIns log)` & then `checkLogIn(String user, String pass)` right? Try removing `db.close();` & only call that in `onDestroy()`. Also are you sure that `LogIns log` is giving you the values you need? Try using the debugger or try putting some manual strings there. – Daniel May 22 '16 at 17:33