如何解决尝试在空对象引用错误时调用虚拟方法的问题

时间:2019-08-05 02:26:12

标签: android android-databinding android-livedata android-jetpack

im试图创建具有MVVM体系结构的登录屏幕并得到java.lang.NullPointerException:尝试在null上调用虚拟方法'androidx.lifecycle.MutableLiveData com.log.ui.login.LoginViewModel.getUser()'对象引用

请帮助查明此错误的原因。我认为问题与Viewmodel有关。任何对此的建议将不胜感激。

我的登录片段代码

public class LoginScreen extends Fragment {


    private LoginViewModel loginViewModel;

   private FragmentLoginBinding binding;

   public View onCreateView(LayoutInflater inflater,
                            @Nullable ViewGroup container,
                            @Nullable Bundle savedInstanceState){

     //  binding=FragmentLoginBinding.inflate(getLayoutInflater(),container,false);
    binding= DataBindingUtil.inflate(inflater,R.layout.fragment_login,container,false);
       View view = binding.getRoot();


       binding.setLifecycleOwner(this);
       binding.setLoginViewModel(loginViewModel);
       //here data must be an instance of the class MarsDataProvider




 loginViewModel.getUser().observe(this,new Observer<Repository>(){

     public void onChanged(Repository loginUser) {

         if (TextUtils.isEmpty(Objects.requireNonNull(loginUser).getStrEmailAddress())) {
             binding.txtEmailAddress.setError("Enter an E-Mail Address");
             binding.txtEmailAddress.requestFocus();
         }
         else if (!loginUser.isEmailValid()) {
             binding.txtEmailAddress.setError("Enter a Valid E-mail Address");
             binding.txtEmailAddress.requestFocus();
         }
         else if (TextUtils.isEmpty(Objects.requireNonNull(loginUser).getStrPassword())) {
             binding.txtPassword.setError("Enter a Password");
             binding.txtPassword.requestFocus();
         }
         else if (!loginUser.isPasswordLengthGreaterThan5()) {
             binding.txtPassword.setError("Enter at least 6 Digit password");
             binding.txtPassword.requestFocus();
         }
         else {
             binding.lblEmailAnswer.setText(loginUser.getStrEmailAddress());
             binding.lblPasswordAnswer.setText(loginUser.getStrPassword());
         }

     }

       });

      // return binding.getRoot();
       return view;
   }
}

LoginViewModel.Java

public class LoginViewModel extends ViewModel {

    public MutableLiveData<String> EmailAddress = new MutableLiveData<>();
    public MutableLiveData<String> Password = new MutableLiveData<>();

    private MutableLiveData<Repository> userMutableLiveData;

    public MutableLiveData<Repository> getUser() {

        if (userMutableLiveData == null) {
            userMutableLiveData = new MutableLiveData<>();
        }
        return userMutableLiveData;

    }

    public void onClick(View view) {

        Repository loginUser = new Repository(EmailAddress.getValue(), Password.getValue());

        userMutableLiveData.setValue(loginUser);

    }

}

我的模型类可以在这里找到。

public class Repository {

    private String strEmailAddress;
    private String strPassword;

    public Repository(String EmailAddress, String Password) {
        strEmailAddress = EmailAddress;
        strPassword = Password;
    }

    public String getStrEmailAddress() {
        return strEmailAddress;
    }

    public String getStrPassword() {
        return strPassword;
    }

    public boolean isEmailValid() {
        return Patterns.EMAIL_ADDRESS.matcher(getStrEmailAddress()).matches();
    }


    public boolean isPasswordLengthGreaterThan5() {
        return getStrPassword().length() > 5;
    }

}

1 个答案:

答案 0 :(得分:0)

据我所知,您尚未初始化视图模型。将以下行放在setLoginViewModel

之前
LoginViewModel loginViewModel = ViewModelProviders.of(getActivity()).get(LoginViewModel.class)