Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How should I design an Address and a Person class

 
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?



 
Marshal
Posts: 67430
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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
Marshal
Posts: 67430
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • 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.

 
Marshal
Posts: 69894
278
  • 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.
 
Bartender
Posts: 1051
5
Hibernate Eclipse IDE Chrome
  • 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: 69894
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
    Bookmark Topic Watch Topic
  • New Topic