这是我的getUser方法:
public User getUser(String Email) {
SQLiteDatabase db = this.getReadableDatabase();
String sql = "SELECT * FROM " + DBContract.Users.TABLE_NAME
+ " WHERE " + DBContract.Users.COL_EMAIL + " = " + Email;
try {
Cursor cursor = db.rawQuery(sql, null);
User user = new User();
// Read data, I simplify cursor in one line
if (cursor != null) {
cursor.moveToFirst();
// Get imageData in byte[]. Easy, right?
user.setUserID(Integer.parseInt(cursor.getString(cursor.getColumnIndex(DBContract.Users._ID))));
user.setUserName(cursor.getString(cursor.getColumnIndex(DBContract.Users.COL_NAME)));
user.setUserImage(cursor.getBlob(cursor.getColumnIndex(DBContract.Users.COL_IMAGE)));
user.setUserPassword(cursor.getString(cursor.getColumnIndex(DBContract.Users.COL_PASSWORD)));
user.setUserEmail(cursor.getString(cursor.getColumnIndex(DBContract.Users.COL_EMAIL)));
user.setUserLocation(cursor.getString(cursor.getColumnIndex(DBContract.Users.COL_LOCATION)));
}
cursor.close();
db.close();
return user;
} catch (SQLException e) {
}
return null;
}
用户个人资料片段我调用getUser:
public class UserHomeFragment extends Fragment {
DBHelper dbHelper;
User user;
ImageView userImageView;
TextView userNameTexView;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.user_profile_home_fragment, container, false);
dbHelper = new DBHelper(v.getContext());
user = new User();
userImageView = (ImageView) v.findViewById(R.id.userimg);
userNameTexView = (TextView) v.findViewById(R.id.user_name);
Bundle extras = getActivity().getIntent().getExtras();
assert extras != null;
String userEmail = extras.getString("userEmail");
user = dbHelper.getUser(userEmail);
// Bitmap bitmap = BitmapFactory.decodeByteArray(userImage, 0, userImage.length);
// userImageView.setImageBitmap(bitmap);
userNameTexView.setText(user.getUserName());
return v;
}}
为什么getUser返回null?我检查过用户桌面上有用户。
错误按摩:尝试在空对象引用上调用虚方法'java.lang.String com.arfni.ayafinal.model.User.getUserName()'
答案 0 :(得分:0)
您遇到的问题是双重的。
主要错误是您需要将字符串值括在引号中,否则可能会将其视为关键字或实体(例如表或列名),否则可能会导致语法错误。简而言之,SQL不会执行并导致异常。
复合错误是异常被捕获,因此异常被忽略导致没有创建和返回Cursor,因此从不设置Cursor变量,因此它将为null然后当您尝试关闭Cursor时它将为null,因为它超出了null检查的范围。
此外,您还有一个潜在的问题,如果游标不是null但是为空,那么moveToFirst
将不会移动到第一行,因为没有第一行要移动到。返回false的moveToFirst
方法表示这一点。您的代码中未检查返回的值,因此当您尝试获取数据时,将发生异常(当有0行时尝试读取索引0)这也会被困,并可能导致混淆。
我建议用以下内容替换getUser方法: -
public User getUser(String Email) {
SQLiteDatabase db = this.getReadableDatabase();
String sql = "SELECT * FROM " + DBContract.Users.TABLE_NAME
+ " WHERE " + DBContract.Users.COL_EMAIL + " = " + Email;
Cursor cursor = db.rawQuery(sql, null);
User user = new User();
// Read data, I simplify cursor in one line
if (cursor.moveToFirst()) {
// Get imageData in byte[]. Easy, right?
user.setUserID(Integer.parseInt(cursor.getString(cursor.getColumnIndex(DBContract.Users._ID))));
user.setUserName(cursor.getString(cursor.getColumnIndex(DBContract.Users.COL_NAME)));
user.setUserImage(cursor.getBlob(cursor.getColumnIndex(DBContract.Users.COL_IMAGE)));
user.setUserPassword(cursor.getString(cursor.getColumnIndex(DBContract.Users.COL_PASSWORD)));
user.setUserEmail(cursor.getString(cursor.getColumnIndex(DBContract.Users.COL_EMAIL)));
user.setUserLocation(cursor.getString(cursor.getColumnIndex(DBContract.Users.COL_LOCATION)));
}
cursor.close();
db.close();
return user;
}