E/AndroidRuntime: FATAL EXCEPTION: main Process: home.partyfinder3, PID: 8459 java.lang.IllegalArgumentException: Cannot bind argument at index 2 because the index is out of range. The statement has 0 parameters. at android.database.sqlite.SQLiteProgram.bind(SQLiteProgram.java:212) at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:166) at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:200) at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47) at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316) at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1255) at home.partyfinder3.LoginActivity.onClick(LoginActivity.java:51) at android.view.View.performClick(View.java:4780) at android.view.View$PerformClick.run(View.java:19866) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 

Here such exception takes off when I press the Enter button. Handler:

 public void onClick(View v) { String username = etUsername.getText().toString(); String password = etPassword.getText().toString(); db = dbHelper.getWritableDatabase(); switch (v.getId()) { case R.id.btnLogin: Cursor c = db.rawQuery("SELECT username FROM users WHERE username='?' AND password='?'", new String[] {username, password}); if(c.moveToFirst()) { Toast.makeText(this, R.string.login_enjoy, Toast.LENGTH_LONG).show(); startActivity(new Intent(this, MainActivity.class).putExtra("username", username)); c.close(); } else { Toast.makeText(this, R.string.login_error, Toast.LENGTH_LONG).show(); } break; case R.id.btnLinkToRegisterScreen: startActivity(new Intent(this, RegisterActivity.class)); break; } dbHelper.close(); } 

Where is something wrong?

  • And if from the text of the request to remove single quotes? - post_zeew
  • It seems to help, I will confirm when I correct another activation - Kolobok163rus
  • Krch, yes, because of the apostrophes, there was all nonsense, thanks @post_zeew - Kolobok163rus
  • Published as an answer. - post_zeew

2 answers 2

Remove single quotes from the query text.

    Make a console-log of the generated query to the database and try to copy it and execute it manually directly in the sql database interface. If the request does not work there, then at least the real reason will be visible.