关于java.lang.IllegalArgumentException: the bind value at index 1 is null的解决办法
今日在写小项目时出现此问题,原因是因为没有获取sp中储存的登录用户名
//从sp种获取登录时的用户名
spUserName = AnalysisUtils.readLoginUserName(this);
关于AnalysisUtils类
//从sp 中获取登录时的用户名
public class AnalysisUtils {
public static String readLoginUserName(Context context){
SharedPreferences sp = context.getSharedPreferences("username", Context.MODE_PRIVATE);
String userName = sp.getString("loginUserName","");
return userName;
}
在需要引用的时候出现问题
bean = DBUtils.getInstance(this).getUserInfo(spUserName);
究其根源出现在获取个人资料的方法里需要用到spUserName
//获取个人资料
public UserBean getUserInfo(String userName){
String sql = "SELECT * FROM "+ SQLiteHelper.U_USERINFO +" WHERE userName = ?";
Cursor cursor = db.rawQuery(sql, new String[]{userName});
UserBean bean = null;
while (cursor.moveToNext()){
bean = new UserBean();
bean.userName = cursor.getString(cursor.getColumnIndex("userName"));
bean.nickName = cursor.getString(cursor.getColumnIndex("nickName"));
bean.sex = cursor.getString(cursor.getColumnIndex("sex"));
bean.signature = cursor.getString(cursor.getColumnIndex("signature"));
}
cursor.close();
return bean;
}
粗心阴差阳错出现了此问题
//此处以上是我自己的问题
导致其出现还有sql语句的问题,出现此问题,优先查看自己的sql语句
//此处为第一种查询方法
public static final String U_USERINFO = "userinfo";
String sql = "SELECT * FROM "+ SQLiteHelper.U_USERINFO +" WHERE userName = ?";
Cursor cursor = db.rawQuery(sql, new String[]{userName});
//此处为第二种写法
Cursor cursor = db.query(SQLiteHelper.U_USERINFO, null,"userName = ?", new String[]{userName}, null, null, null);
本文解析了在Android项目中遇到的java.lang.IllegalArgumentException:thebindvalueatindex1isnull错误,详细阐述了问题根源在于未从SP读取登录用户名,并提供了具体的解决步骤,包括正确使用SharedPreferences获取数据及SQL查询语句的检查。
1152

被折叠的 条评论
为什么被折叠?



