Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to allow null to an object attribute?

 
Danilo Dadonas
Ranch Hand
Posts: 59
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, all. There are 2 enitities: EntityOne and EntityTwo.



When the application loads and EntityOne object and the attribute entityTwo is not null, everything goes fine. But when I try to retrieve an EntityOne object and the attribute entityTwo is null, the application throws a NullPointerException.

If the attribute is allowed to be null, why an exception is occuring?

Thanks...
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you paste the code and the Exception trace.

Mark
 
Danilo Dadonas
Ranch Hand
Posts: 59
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Code to load the object



In this case, the trader of this object is null. But intead of take an exception I'd like to take an empty String, without catch the exception manually.

The exception:

 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the NullPointerException is caused by you trying to access an associated object when there is not associated object there? Could you not just get round this with a simple defensive check?
 
Danilo Dadonas
Ranch Hand
Posts: 59
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Paul. I could but this attribute can be null. I will need to acces this attribute in the future to show the trader's name in a form. Will I have to implement checks for all attributes I think can be null?
Why it not work?

If I can insert null attributes easily, why can't I retrive it easily too?
 
Paul Clapham
Sheriff
Posts: 21581
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can retrieve a null value easily. But your question is about what happens after you retrieve the null value. You're asking for a change to the Java language such that when you call a method that returns a String on a null reference, it shouldn't crash, it should return an empty String. And that isn't going to happen. So yes, it's up to you to handle nulls appropriately for your application.
 
Danilo Dadonas
Ranch Hand
Posts: 59
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For each nullable attribute I'll have to check like the following code?:



My intencion is when I retrieving the object from the database to show in a form, the null attributes be tabled as "".
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, similar to that code, you are missing a "()" in the if part of the statement.

See if you call rep.getTrader() and it returns null, you can't call a method on null.

rep.getTrader().getName());

so the first part returns null, then you try to call getName() on null, that will always throw a Null pointer exception. Because getTrader() might return null, you will have to check for null.

Mark
 
Danilo Dadonas
Ranch Hand
Posts: 59
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, Mark. Than, I'll return a new object when it's null. Like this:



Is it a good practice?

Thanks.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wouldn't do it. If there is no associated object then I would expect the association to be null. What happens when later on you try to persist this non-existant object? Best case scenario you create a relationship where one shouldn't exist.
[ March 13, 2008: Message edited by: Paul Sturrock ]
 
Danilo Dadonas
Ranch Hand
Posts: 59
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok, Paul, I agree. Won't I have problem if I use ActionForm to retrieve this object with null properties?
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That depends on how you implement your ActionForm.
 
Danilo Dadonas
Ranch Hand
Posts: 59
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll try. After, I'll post the code.

Thanks.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic