Currently I'm developing a mobile application in Android Studio (Java). I want to make a SQLite-database with account information of users. To log in, the user credentials (username + password) need to be verified. In order to do this, I want to compare the string from an EditText with the credentials stored in the database. When the user touches/presses the button the method 'check' is called. Down below, you can see how this works (in the MainActivity):
public class MainActivity extends AppCompatActivity {
TextView TV1,TV2;
EditText et_username, et_password;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TV1 = (TextView) findViewById(R.id.TV1);
TV1.setText("Please fill in your username and password.");
TV1.setTextColor(Color.BLUE);
TV2 = (TextView) findViewById(R.id.TV2);
TV2.setTextColor(Color.RED);
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS Account(" +
"account_id INTEGER PRIMARY KEY AUTOINCREMENT," +
"username TEXT NOT NULL," +
"password TEXT NOT NULL);");
db.execSQL("INSERT INTO Account (username,password) VALUES ('Simon','123');");
db.execSQL("INSERT INTO Account (username,password) VALUES ('Bart','abc');");
db.execSQL("INSERT INTO Account (username,password) VALUES ('Sytze','lol');");
}
public void check(View view){
String username = et_username.getText().toString();
String password = et_password.getText().toString();
Cursor resultSet = db.rawQuery("SELECT username,password FROM Account WHERE username='" + username + "';", null);
resultSet.moveToFirst();
String gbTest = resultSet.getString(resultSet.getColumnIndex("username"));
String wwTest = resultSet.getString(resultSet.getColumnIndex("password"));
resultSet.close();
String res = gbTest + " : " + wwTest;
TV2.setText(res);
/*
if (password.toString() != wwTest){
TV2.setText("Wrong combination...");
}
*/
}
}
When I press the button I get an error (i.e. the app crashes). However, when I replace the 'username' in the resultSet.rawQuery with an actual username in the database, like 'Bart', it does work. Apparently, there is a difference between the string of the EditText and an actual string. How does this come, and why can't I use the string of the EditText? I hope my problem and question are clear, but if not: please let me know.
Thanks in advance for your help.
Greets, Sytze
EDIT This is the error I get:
06-21 15:29:34.194 2114-2114/sytzesimonse.databaseapp E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:275)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17749)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5473)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:854)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17749)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5473)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:854)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
at android.database.AbstractCursor.checkPosition(AbstractCursor.java:427)
at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:50)
at sytzesimonse.databaseapp.MainActivity.check(MainActivity.java:56)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:270)
at android.view.View.performClick(View.java:4240)
at android.view.View$PerformClick.run(View.java:17749)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5473)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:854)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
at dalvik.system.NativeStart.main(Native Method)