I am trying to make a login page in Android, but the program crashes every time I run it. Here's my code:
b1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String a = ed1.getText().toString();
String b = ed2.getText().toString();
String repassword;
if (a.length() <= 0 || b.length() <= 0) {
if (a.length() <= 0) {
ed1.setError("Please enter your username.");
}
if (b.length() <= 0) {
ed2.setError("Please enter your password.");
}
} else {
Cursor cursor = sq
.query("AccountRegistration", null, " Username=?",
new String[] { a }, null, null, null);
if (cursor.getCount() < 1) {
cursor.close();
repassword = "NOT EXIST";
}
cursor.moveToFirst();
repassword = cursor.getString(cursor
.getColumnIndex("Password"));
cursor.close();
String storedPassword = repassword;
if (b.equals(storedPassword)) {
Toast.makeText(getApplicationContext(),
"You have been logged in.", Toast.LENGTH_LONG)
.show();
Intent ii = new Intent(MainActivity.this,
WelcomePage.class);
startActivity(ii);
} else {
Toast.makeText(
getApplicationContext(),
"Please check your credentials or create a new account.",
Toast.LENGTH_LONG).show();
ed1.setText("");
ed2.setText("");
}
}
}
});
When I tried debugging, I noticed the error was at the part where I store cursor.getString(cursor.getColumnIndex("Password")) into the repassword variable. My logcat says cursorindexoutofbounds.