I would like to display a sortable list of restaurants. It worked when I was using a simple LiveData, but I get a NullPointerException since I changed it for a mediatorLiveData.
In the fragment, i use these lines to observe the mediatorLiveData
vm.getAllRestaurantsWithOrderMediatorLD().observe(getViewLifecycleOwner(), listRestaurants -> {
datas.clear();
datas.addAll(listRestaurants);
adapter.notifyDataSetChanged();
});
And here the viewmodel class :
public class ListRestaurantsViewModel extends ViewModel {
private final RestaurantRepository restaurantRepository;
private final SortRepository sortRepository;
private final MutableLiveData<List<RestaurantViewState>> allRestaurantsViewStateLD = new MutableLiveData<>();
private final MutableLiveData<SortRepository.OrderBy> orderLiveData = new MutableLiveData<>();
private final MediatorLiveData<List<RestaurantViewState>> allRestaurantsWithOrderMediatorLD;
Context ctx;
public ListRestaurantsViewModel(RestaurantRepository restaurantRepository, SortRepository sortRepository, Context ctx) {
this.ctx = ctx;
this.restaurantRepository = restaurantRepository;
this.sortRepository = sortRepository;
allRestaurantsWithOrderMediatorLD = new MediatorLiveData<>();
allRestaurantsWithOrderMediatorLD.addSource(getAllRestaurantsViewStateLD(), value -> allRestaurantsWithOrderMediatorLD.setValue(value));
allRestaurantsWithOrderMediatorLD.addSource(getOrderLiveData(), order -> {
List<RestaurantViewState> restaurants = getAllRestaurantsViewStateLD().getValue();
if (restaurants != null && !restaurants.isEmpty()) {
List<RestaurantViewState> newList = new ArrayList<>();
if (order == SortRepository.OrderBy.DISTANCE)
newList =
Stream.of(restaurants).sorted((a, b) -> a.getDistance() - b.getDistance()).toList();
else if (order == SortRepository.OrderBy.RATING)
newList = Stream.of(restaurants).sorted((a, b) -> Double.compare(a.getStarsCount(),
b.getStarsCount())).toList();
allRestaurantsWithOrderMediatorLD.setValue(newList);
}
}
);
}
public MediatorLiveData<List<RestaurantViewState>> getAllRestaurantsWithOrderMediatorLD() {
return allRestaurantsWithOrderMediatorLD;
}
public LiveData<SortRepository.OrderBy> getOrderLiveData() {
return sortRepository.getOrderLiveData();
}
public LiveData<List<RestaurantViewState>> getAllRestaurantsViewStateLD() {
return Transformations.map(restaurantRepository.getRestaurantsLiveData(), restaurantsList -> {
List<RestaurantViewState> restaurantViewStates = new ArrayList<>();
for (Restaurant r : restaurantsList) {
restaurantViewStates.add(new RestaurantViewState(
r.getId(),
r.getName(),
r.getType(),
r.getAdress(),
r.getImage()
)
);
}
return restaurantViewStates;
});
}
This is the error, I get
java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.lifecycle.LiveData.observeForever(androidx.lifecycle.Observer)' on a null object reference
I have no error if I observe the livedata getAllRestaurantsViewStateLD() in the fragment, so I think I did not get something in the use of mediatorLiveData Do you see what it is?
The crash is on the call to
observeForever, but your code snippet doesn't show you are using it. So the problem is somewhere else.