title: How to fix Attempt to invoke virtual method on a null object reference error
body: <p>im trying to create a login screen with MVVM architecture and got a java.lang.NullPointerException: Attempt to invoke virtual method 'androidx.lifecycle.MutableLiveData com.log.ui.login.LoginViewModel.getUser()' on a null object reference</p>
  
  <p>Please help to pinpoint the cause of this error. I think issue is something to do with Viewmodel. Any suggestion regarding this would be highly appreciated.</p>
  
  <p>My Login Fragment code</p>
  
  <pre><code>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&lt;Repository&gt;(){
  
       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;
     }
  }
  </code></pre>
  
  <p>LoginViewModel.Java </p>
  
  <pre><code>public class LoginViewModel extends ViewModel {
  
      public MutableLiveData&lt;String&gt; EmailAddress = new MutableLiveData&lt;&gt;();
      public MutableLiveData&lt;String&gt; Password = new MutableLiveData&lt;&gt;();
  
      private MutableLiveData&lt;Repository&gt; userMutableLiveData;
  
      public MutableLiveData&lt;Repository&gt; getUser() {
  
          if (userMutableLiveData == null) {
              userMutableLiveData = new MutableLiveData&lt;&gt;();
          }
          return userMutableLiveData;
  
      }
  
      public void onClick(View view) {
  
          Repository loginUser = new Repository(EmailAddress.getValue(), Password.getValue());
  
          userMutableLiveData.setValue(loginUser);
  
      }
  
  }
  </code></pre>
  
  <p>My Modelclass can be found here.</p>
  
  <pre><code>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() &gt; 5;
      }
  
  }
  </code></pre>