在Room实现中获取nullpointer异常

时间:2018-11-01 13:42:36

标签: android-room

我是android新手,正在实现Room库。我已经向数据库中输入了一些数据,但是当我检查数据是否存在时,没有任何结果。

这是Dao界面:

public interface UserDao {

@Insert
void insert(User user);

@Query("SELECT * FROM word_table WHERE phoneNo LIKE phoneNo = :phone + password LIKE password = :password")
User findUserWithPhoneAndPass(String phone, String password);

}

这是存储库:

public class UserRepository {

private UserDao mUserDao;
private User userWithPhone;

UserRepository(Application application){
    UserRoomDatabase userRoomDatabase = 
   UserRoomDatabase.getDatabase(application);
    mUserDao = userRoomDatabase.userDao();

}



void insert(User user){
    new insertAsyncTask(mUserDao).execute(user);
}


public void insertTask(String firstName, String lastName, String email, 
    String phone, String password){

    User user = new User();
    user.setFirstName(firstName);
    user.setLastName(lastName);
    user.setEmail(email);
    user.setPhoneNo(phone);
    user.setPassword(password);
    insertTask(user);
}

private void insertTask(final User user){
    new AsyncTask<Void, Void, Void>(){
        @Override
        protected Void doInBackground(Void... voids) {
            mUserDao.insert(user);
            return null;
        }
    };
}

public User checkUserWithPhone(String ph, String p){
    return mUserDao.findUserWithPhoneAndPass(ph, p);
}

private static class insertAsyncTask extends AsyncTask<User, Void, Void> {

    private UserDao mUserAsyncDao;
    insertAsyncTask(UserDao dao){
        mUserAsyncDao = dao;
    }

    @Override
    protected Void doInBackground(User... users) {
        mUserAsyncDao.insert(users[0]);
        return null;
    }
}

}

这是ViewModel类:

public class UserViewModel extends AndroidViewModel {
private UserRepository mRepository;

public UserViewModel(@NonNull Application application) {
    super(application);
    mRepository = new UserRepository(application);

public void insertTask(String f, String l, String e, String ph, String p){
    mRepository.insertTask(f,l,e,ph,p);
}

public User findUser(String ph, String p){
    return mRepository.checkUserWithPhone(ph, p);
}

这是注册课程:

public class SignUp extends AppCompatActivity implements View.OnClickListener {

private InputValidation inputValidation;
private UserViewModel mViewModel;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sign_up);

    inputValidation = new InputValidation(this);

    mViewModel = ViewModelProviders.of(this).get(UserViewModel.class);
}

@Override
public void onClick(View v) {
    switch (v.getId()){
        case R.id.joinNow:

  mVieModel.insertTask(firstname.getText().toString(),
  lastName.getText().toString,email.getText().toString(),
  phone.getText().toString(),password.getText().toString());

            Toast.makeText(this, "New user created Successfully", 
 Toast.LENGTH_SHORT).show();
            break;
    }
    finish();
}

}

这是登录类:

public void launchMainScreen(View view){
    dao = UserRoomDatabase.getDatabase(getApplicationContext()).userDao();

    String phoneNo = phone.getText().toString();
    String userPass = password.getText().toString();
    User user = model.findUser(phoneNo,userPass);
    String phone = user.getPhoneNo();

    if (phone != null){
        Intent intent = new Intent(this, DashBoardActivity.class);
        startActivity(intent);
    }else {
        Toast.makeText(this, "Please enter a valid Phone No. or Password", Toast.LENGTH_SHORT).show();
    }

}

我在一行中得到空结果:user = model.findUser(phoneNo,userPass)

0 个答案:

没有答案