• Post Reply Bookmark Topic Watch Topic
  • New Topic

How should I design an Address and a Person class  RSS feed

 
Brian Storenson
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm trying to create a demo website as an exercise. I want to be able to create a Person.class, and an Address.class. I want to associate an address to a person. I want to be able to create a Person without having to create an Address at the same time. I am getting a null pointer exception when I try to create a Person, because my Address is null. How should I design this?



 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where are you getting the NPE? Don't make people guess.
 
Brian Storenson
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 18 of the Person.class I tried to create an instance to deal with the null pointer exception. It was originally like this:

 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66306
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please show us the exact stack trace. There is no way that that line can create an NPE.
 
Brian Storenson
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bear Bibeault wrote:Where are you getting the NPE? Don't make people guess.


I think it is when I call the getPersonAddress(). I changed the method so that it would check to see if the Object was null. Now it looks like this, and it works.

 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is something wrong about having the address field as an Address object, and returning a String from the getAddress method. That breaches the principle that each class takes responsibility for itself. you should have a toString method overridden in the Address class, and the getAddress method should return an Address object.
 
Brian Storenson
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:There is something wrong about having the address field as an Address object, and returning a String from the getAddress method. That breaches the principle that each class takes responsibility for itself. You should have a toString method overridden in the Address class, and the getAddress method should return an Address object.



Thank you! I was thinking that was poor OO design, but I didn't know the proper way to implement.
 
James Boswell
Bartender
Posts: 1051
5
Chrome Eclipse IDE Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with Campbell. Instead of calling:

use:

I would remove the getPersonAddress method altogether and implement similar logic in the toString method of Address.

Also, consider the following:
- Rename streetAddress to street within the Address class
- Addresses usually have a house number or name so perhaps houseNumber should be renamed. It should also be a String instead of an int in this case
- As you are using JPA annotations, you should probably consider adding @OneToOne to the address within the Person class

 
Brian Storenson
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Boswell wrote:I agree with Campbell. Instead of calling:

use:

I would remove the getPersonAddress method altogether and implement similar logic in the toString method of Address.

Also, consider the following:
- Rename streetAddress to street within the Address class
- Addresses usually have a house number or name so perhaps houseNumber should be renamed. It should also be a String instead of an int in this case
- As you are using JPA annotations, you should probably consider adding @OneToOne to the address within the Person class



Thank you for the helpful comments!
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!