• 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

Can the following expression generate null pointer exception?

 
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear All,

I have a java statement like this in my DAO layer...

outputDTO.setCost(Commas(Round(Double.parseDouble(rs.getString("MATERIAL_COST")))));

In this I am sure that outputDTO and rs objects are NOT NULL definitely.
That being the case, is there a possibility for me to encounter "Null pointer exception"

Why I am asking this is, the customer is facing a NPException in the prod environment while I am unable to replicate the same in my local environment. The "MATERIAL_COST" field in database table is of type NUMBER(15,3).
When I got the logs from the production, the null pointer exception was pointing this line only.

Please help me out.

Regards,
Sriram.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes. What if this piece is null. Null is certainly a valid value for a database.
 
Jeanne Boyarsky
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also, is there any reason you aren't calling rs.getDouble("MATERIAL_COST")? That will return 0.0 if it is null and avoid this problem.
 
Sriram Sharma
Ranch Hand
Posts: 145
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your reply Jeanne...
So, are you saying that in the current expression, if the "MATERIAL_COST" value in database is blank (null), we could encounter a null pointer exception?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sriram,

Yes, of course, it will throw a null pointer exception, if the value from the database is null.
After all, you are trying to oprate on an object whose value is null. Aren't you?

And please follow Jeanne's comments if you would like to avoid the problem, if it suits your requirement.
 
Message for you sir! I think it is a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic