Im getting the following error
Im using Android architecture components and tried to instantiate viewmodel and Observe data from LiveData. But im stuck here please help me solve this issue :
Attempt to invoke virtual method 'void android.arch.lifecycle.LiveData.observe(android.arch.lifecycle.LifecycleOwner, android.arch.lifecycle.Observer)' on a null object reference
My Fragment is this : While calling getCompany() method im getting and NullPoint Exception Error
public class CompanyFragment extends LifecycleFragment {
private CompanyViewModel companyViewModel;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
companyViewModel = ViewModelProviders.of(this).get(CompanyViewModel.class);
companyViewModel.initData();
companyViewModel.getCompany().observe(this, new Observer<CompanyEntity>() {
@Override
public void onChanged(@Nullable CompanyEntity companyEntity) {
}
});
}
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_company, container, false);
}
}
My ViewModel class is this :
public class CompanyViewModel extends ViewModel {
private LiveData<CompanyEntity> companyEntityLiveData;
private CompanyRepository companyRepo;
public CompanyViewModel() {}
public CompanyViewModel(CompanyRepository companyRepo){
this.companyRepo = companyRepo;
}
public void initData(){
companyEntityLiveData = companyRepo.getCompany();
}
public LiveData<CompanyEntity> getCompany() {
return companyEntityLiveData;
}
}
Error is at this line :
companyViewModel.getCompany().observe(this, new Observer<CompanyEntity>() {
@Override
public void onChanged(@Nullable CompanyEntity companyEntity) {
}
});
This is my CompanyRepository class:
public class CompanyRepository {
private WebService webService;
public CompanyRepository(){
}
public LiveData<CompanyEntity> getCompany() {
final MutableLiveData<CompanyEntity> data = new MutableLiveData<>();
webService.getCompany().enqueue(new Callback<CompanyEntity>() {
@Override
public void onResponse(Call<CompanyEntity> call, Response<CompanyEntity> response) {
data.setValue(response.body());
}
@Override
public void onFailure(Call<CompanyEntity> call, Throwable t) {
}
});
return data;
}
}
You must observe the same instance of your LiveData. In your CompanyRepository
class you create a new LiveData instance every time you access the company, which means your activity is then observing an old instance.
The getCompany method should be changed to always return the same instance of the LiveData. Also, you should change setValue() to postValue() because you are trying to update the value on a background thread.
final MutableLiveData<CompanyEntity> data = new MutableLiveData<>();
public LiveData<CompanyEntity> getCompany() {
webService.getCompany().enqueue(new Callback<CompanyEntity>() {
@Override
public void onResponse(Call<CompanyEntity> call, Response<CompanyEntity> response) {
data.postValue(response.body());
}
@Override
public void onFailure(Call<CompanyEntity> call, Throwable t) {
}
});
return data;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With