java.lang.IllegalArgumentExceptionIllegalArgumentException: 索引 2 处的绑定值为空

java.lang.IllegalArgumentExceptionIllegalArgumentException: the bind value at index 2 is null

我试图通过获取 cursor.getString() 并将其设置为 Textview 来访问和显示存储在数据库中的详细信息。 我有点坚持这样做并帮助我

Databasehelper.java

public Cursor checkduplicates_in_user_credentials(String user_name, String password, String table) {
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor res = null;

        if (table.equals(TABLE_NAME_USER)) {
            res = db.rawQuery("select * from " + TABLE_NAME_USER  + " where username=? and password=? ", new String[]{user_name, password},null);
        }
        return res;
    }

运行 应用

时出错
    Process: com.example.instacure, PID: 11556
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.instacure/com.example.instacure.Personal_Info}: java.lang.IllegalArgumentException: the bind value at index 2 is null
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.IllegalArgumentException: the bind value at index 2 is null
        at android.database.sqlite.SQLiteProgram.bindString(SQLiteProgram.java:167)
        at android.database.sqlite.SQLiteProgram.bindAllArgsAsStrings(SQLiteProgram.java:203)
        at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:49)
        at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1443)
        at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1400)
        at com.example.instacure.DatabaseHelper.checkduplicates_in_user_credentials(DatabaseHelper.java:72)
        at com.example.instacure.Personal_Info.onCreate(Personal_Info.java:44)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

这是它引用的地方

PersonalInfo.java

 Cursor y = db.checkduplicates_in_user_credentials(username, password, getResources().getString(R.string.user_credentials));


        if (y.moveToFirst()) {
            String name1 = y.getString(y.getColumnIndex("first_name"));
            String name2 = y.getString(y.getColumnIndex("last_name"));

            name.setText(name1+" "+name2);
            age.setText(y.getString(y.getColumnIndex("age")));
            email.setText(y.getString(y.getColumnIndex("e_mail")));
            pwd.setText(y.getString(y.getColumnIndex("password")));
            usrname.setText(y.getString(y.getColumnIndex("username")));
            phoneno.setText(y.getString(y.getColumnIndex("phone_number")));
            bloodgroup.setText(y.getString(y.getColumnIndex("blood_group")));
            gender.setText(y.getString(y.getColumnIndex("gender")));
            city.setText(y.getString(y.getColumnIndex("city")));
            utype.setText(y.getString(y.getColumnIndex("user_type")));


        }

java.lang.IllegalArgumentException: the bind value at index 2 is null 告诉你 passwordnull 因此不能用来替换(绑定)查询中的?.

您需要确保在获取密码时绝不会为空或将其处理为空。

正确的应该是第一个(没有提供获取密码的代码,所以无法给出这个修复)。但是以下(将其处理为空)可以解决潜在问题:-

public Cursor checkduplicates_in_user_credentials(String user_name, String password, String table) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = null;
    if (password == null) password = "apassswordthatwouldneverbeusedbytheuser";
    if (table.equals(TABLE_NAME_USER)) {
        res = db.rawQuery("select * from " + TABLE_NAME_USER  + " where username=? and password=? ", new String[]{user_name, password},null);
    }
    return res;
}

但是,可能仍然存在问题,因为调用代码不处理空游标,如果 table 不等于 TABLE_NAME_USER。

可能会发生这种情况

所以你应该有这样的东西:-

    Cursor y = db.checkduplicates_in_user_credentials(username, password, getResources().getString(R.string.user_credentials));

    if (y != null && y.moveToFirst()) { // will not try to moveToFirst if the Cursor is null
        String name1 = y.getString(y.getColumnIndex("first_name"));
        String name2 = y.getString(y.getColumnIndex("last_name"));

        name.setText(name1+" "+name2);
        age.setText(y.getString(y.getColumnIndex("age")));
        email.setText(y.getString(y.getColumnIndex("e_mail")));
        pwd.setText(y.getString(y.getColumnIndex("password")));
        usrname.setText(y.getString(y.getColumnIndex("username")));
        phoneno.setText(y.getString(y.getColumnIndex("phone_number")));
        bloodgroup.setText(y.getString(y.getColumnIndex("blood_group")));
        gender.setText(y.getString(y.getColumnIndex("gender")));
        city.setText(y.getString(y.getColumnIndex("city")));
        utype.setText(y.getString(y.getColumnIndex("user_type")));
    }

既然您正在处理空游标,那么如果密码实际上是 apassswordthatwouldneverbeusedbytheuser 那么您就可以而是使用 :-

public Cursor checkduplicates_in_user_credentials(String user_name, String password, String table) {
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor res = null;
    if (password == null) return null;
    if (table.equals(TABLE_NAME_USER)) {
        res = db.rawQuery("select * from " + TABLE_NAME_USER  + " where username=? and password=? ", new String[]{user_name, password},null);
    }
    return res;
}