Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attempt to invoke virtual method 'void android.arch.lifecycle.MutableLiveData.setValue(java.lang.Object)' on a null object reference

I use Android architecture components and tried to instantiate viewmodel and Observe data from LiveData. But im getting the following error :

ActivationFragment.clas:

@OnClick(R.id.btn_activation)
public void onEvaluateClick(Button v) {
    maActivationCode = etActivation.getText().toString();
    mActiationViewModel.getsatusCode(maActivationCode).observe(this, new Observer<Boolean>() {
        @Override
        public void onChanged(@Nullable Boolean aBoolean) {
        } 
    });
}

ActivationViewModel class:

public class ActivationViewModel  extends ViewModel{

public ActivationRepository activationRepository;

public ActivationViewModel(ActivationRepository activationRepository) {
    this.activationRepository = activationRepository;
}

public LiveData<Boolean> getsatusCode(String activationCode) {
    return (LiveData<Boolean>)  activationRepository.getStatusCode(activationCode);
}

}

ActivationRepository class:

public class ActivationRepository {

public MutableLiveData<Boolean> status;

public MutableLiveData<Boolean> getStatusCode(String activationCode) {
    status.setValue(Boolean.valueOf(false));
    return status;
}
like image 883
Nikolas Bozic Avatar asked Dec 23 '22 09:12

Nikolas Bozic


1 Answers

Update your function like this..

public class ActivationRepository {

public MutableLiveData<Boolean> status = new MutableLiveData<>();

 public MutableLiveData<Boolean> getStatusCode(String activationCode) {
    status.postValue(Boolean.valueOf(false));
    return status;
}
like image 196
Santanu Sur Avatar answered Jan 13 '23 11:01

Santanu Sur