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