• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How to allow null to an object attribute?

 
Ranch Hand
Posts: 59
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you paste the code and the Exception trace.

Mark
 
Danilo Dadonas
Ranch Hand
Posts: 59
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:

 
Bartender
Posts: 10336
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That depends on how you implement your ActionForm.
 
Danilo Dadonas
Ranch Hand
Posts: 59
Hibernate Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'll try. After, I'll post the code.

Thanks.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic