Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can the following expression generate null pointer exception?

 
Sriram Sharma
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
  • 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.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 34084
337
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • 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: 130
  • Mark post as helpful
  • send pies
  • 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?
 
Jack Anderson
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic