aspose file tools*
The moose likes JDBC and the fly likes Can the following expression generate null pointer exception? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Can the following expression generate null pointer exception?" Watch "Can the following expression generate null pointer exception?" New topic
Author

Can the following expression generate null pointer exception?

Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 98
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.
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31075
    
232

Yes. What if this piece is null. Null is certainly a valid value for a database.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 31075
    
232

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

Joined: Apr 12, 2006
Posts: 98
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?
Jack Anderson
Greenhorn

Joined: Dec 19, 2009
Posts: 5
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.
 
Don't get me started about those stupid light bulbs.
 
subject: Can the following expression generate null pointer exception?