Skip to content

Commit

Permalink
handling Null pointer Exception on OwnerID
Browse files Browse the repository at this point in the history
  • Loading branch information
bijomutta authored and dsyer committed Jul 14, 2023
1 parent 3a61275 commit 0a52901
Showing 1 changed file with 12 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,23 @@ public Collection<PetType> populatePetTypes() {

@ModelAttribute("owner")
public Owner findOwner(@PathVariable("ownerId") int ownerId) {
return this.owners.findById(ownerId);

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);

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")
Expand Down

0 comments on commit 0a52901

Please sign in to comment.