gpt4 book ai didi

android - java.lang.IllegalArgumentExceptionIllegalArgumentException : the bind value at index 2 is null 错误

转载 作者:行者123 更新时间:2023-11-30 04:58:44 28 4
gpt4 key购买 nike

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

数据库助手.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) 

这是它引用的地方

个人信息.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 因此不能用于替换(绑定(bind))查询中的?

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

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

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_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;
}

关于android - java.lang.IllegalArgumentExceptionIllegalArgumentException : the bind value at index 2 is null 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58648992/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com