mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-15 12:25:50 +00:00
handling Null pointer Exception on OwnerID
This commit is contained in:
parent
3a6127557d
commit
0a529015bc
1 changed files with 12 additions and 2 deletions
|
@ -56,13 +56,23 @@ class PetController {
|
||||||
|
|
||||||
@ModelAttribute("owner")
|
@ModelAttribute("owner")
|
||||||
public Owner findOwner(@PathVariable("ownerId") int ownerId) {
|
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")
|
@ModelAttribute("pet")
|
||||||
public Pet findPet(@PathVariable("ownerId") int ownerId,
|
public Pet findPet(@PathVariable("ownerId") int ownerId,
|
||||||
@PathVariable(name = "petId", required = false) Integer petId) {
|
@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")
|
@InitBinder("owner")
|
||||||
|
|
Loading…
Reference in a new issue