From 07dd88f9f73900c2a75abe921b47b0288cd9f477 Mon Sep 17 00:00:00 2001 From: bijomutta Date: Thu, 13 Jul 2023 20:37:36 +0200 Subject: [PATCH 1/2] Handling Null Pointer Exception for Owner ID --- .../petclinic/owner/PetController.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java index c3334bab8..fb56002d6 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java @@ -56,13 +56,28 @@ class PetController { @ModelAttribute("owner") public Owner findOwner(@PathVariable("ownerId") int ownerId) { - return this.owners.findById(ownerId); + + if (ownerId <= 0) { + throw new IllegalArgumentException("Owner ID is required"); + } + Owner owner = this.owners.findById(ownerId); + if (owner == null) { + throw new IllegalArgumentException("Owner ID not found: " + ownerId); + } + return owner; } @ModelAttribute("pet") public Pet findPet(@PathVariable("ownerId") int ownerId, @PathVariable(name = "petId", required = false) Integer petId) { - return petId == null ? new Pet() : this.owners.findById(ownerId).getPet(petId); + if (ownerId <= 0) { + throw new IllegalArgumentException("Owner ID is required"); + } + Owner owner = this.owners.findById(ownerId); + if (owner == null) { + throw new IllegalArgumentException("Owner ID not found: " + ownerId); + } + return petId == null ? new Pet() : owner.getPet(petId); } @InitBinder("owner") From 2b6a4ff434c80d303a34b2d2eec5e69339c39ae8 Mon Sep 17 00:00:00 2001 From: bijomutta Date: Fri, 14 Jul 2023 11:03:49 +0200 Subject: [PATCH 2/2] removed <=0 check for ownerID --- .../samples/petclinic/owner/PetController.java | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java index fb56002d6..5dd6c4003 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java @@ -57,9 +57,7 @@ class PetController { @ModelAttribute("owner") public Owner findOwner(@PathVariable("ownerId") int ownerId) { - if (ownerId <= 0) { - throw new IllegalArgumentException("Owner ID is required"); - } + Owner owner = this.owners.findById(ownerId); if (owner == null) { throw new IllegalArgumentException("Owner ID not found: " + ownerId); @@ -70,9 +68,7 @@ class PetController { @ModelAttribute("pet") public Pet findPet(@PathVariable("ownerId") int ownerId, @PathVariable(name = "petId", required = false) Integer petId) { - if (ownerId <= 0) { - throw new IllegalArgumentException("Owner ID is required"); - } + Owner owner = this.owners.findById(ownerId); if (owner == null) { throw new IllegalArgumentException("Owner ID not found: " + ownerId);