fix: rectify a NPE caused by wrong ListView model

Apparently a thing can be selected but not focused. Oops.
This commit is contained in:
Zhongheng Liu 2025-12-04 18:22:10 +01:00
commit 30cbfbc454
Signed by: steven
GPG key ID: F69B980899C1C09D

View file

@ -92,37 +92,36 @@ public class FoodpalApplicationCtrl implements LocaleAware {
this.ingredientListCtrl = ingredientListCtrl;
this.stepListCtrl = stepListCtrl;
}
@Override
public void initializeComponents() {
// TODO Reduce code duplication??
// Initialize callback for ingredient list updates
this.ingredientListCtrl.setUpdateCallback(newList -> {
Recipe focusedRecipe = recipeList.getFocusModel().getFocusedItem();
if (focusedRecipe == null) {
Recipe selectedRecipe = recipeList.getSelectionModel().getSelectedItem();
if (selectedRecipe == null) {
// edge case error for NPE.
throw new NullPointerException("Null recipe whereas ingredients are edited");
}
focusedRecipe.setIngredients(newList);
selectedRecipe.setIngredients(newList);
try {
// propagate changes to server
server.updateRecipe(focusedRecipe);
server.updateRecipe(selectedRecipe);
} catch (IOException | InterruptedException e) {
throw new UpdateException("Unable to update recipe to server for " + focusedRecipe);
throw new UpdateException("Unable to update recipe to server for " + selectedRecipe);
}
});
this.stepListCtrl.setUpdateCallback(newList -> {
Recipe focusedRecipe = recipeList.getFocusModel().getFocusedItem();
if (focusedRecipe == null) {
Recipe selectedRecipe = recipeList.getSelectionModel().getSelectedItem();
if (selectedRecipe == null) {
// edge case error for NPE.
throw new NullPointerException("Null recipe whereas ingredients are edited");
}
focusedRecipe.setPreparationSteps(newList);
selectedRecipe.setPreparationSteps(newList);
try {
// propagate changes to server
server.updateRecipe(focusedRecipe);
server.updateRecipe(selectedRecipe);
} catch (IOException | InterruptedException e) {
throw new UpdateException("Unable to update recipe to server for " + focusedRecipe);
throw new UpdateException("Unable to update recipe to server for " + selectedRecipe);
}
});
// Show recipe name in the list
@ -284,6 +283,7 @@ public class FoodpalApplicationCtrl implements LocaleAware {
try {
server.updateRecipe(selected);
refresh();
recipeList.getSelectionModel().select(selected);
} catch (IOException | InterruptedException e) {
// throw a nice blanket UpdateException
throw new UpdateException("Error occurred when updating recipe name!");