• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 108
  • Last Modified:

Java check not null on double

Dear all,

I'm getting a null pointer exception in the following code block.  I've read I can't check for null if its a double. How do I resolve this?

int sumHotWater = CalculateSumHotWater(hotwater_map);
double hotwater = 0;
if (sumHotWater != 0) {
    hotwater = (applianceTotal.get("Hotwater").doubleValue()); // null pointer exception here
    if (hotwater != 0.0) {
        hotwater /= sumHotWater;
    }
}

Open in new window


Thanks
0
AndyC1000
Asked:
AndyC1000
  • 2
2 Solutions
 
zzynxSoftware engineerCommented:
Since you write
(applianceTotal.get("Hotwater")).doubleValue()

Open in new window

I suppose that
applianceTotal.get("Hotwater")

Open in new window

returns a Double.
Can you confirm that?

Replace

hotwater = (applianceTotal.get("Hotwater").doubleValue());

Open in new window

by
if ( applianceTotal.get("Hotwater") != null ) {
   hotwater = (applianceTotal.get("Hotwater").doubleValue());
}

Open in new window

0
 
Radek BaranowskiFull-stack Java DeveloperCommented:
check if this:

applianceTotal.get("Hotwater")

yields you anything. what do you expect it to get you ?
0
 
CEHJCommented:
If it did return Double, you could probably just rely on auto-unboxing, so i'm guessing it returns Number. Either way - no need to call get twice, so

Number hotWaterValue = applianceTotal.get("Hotwater");
if (hotWaterValue != null) {
   // Do it
}

Open in new window

If you know it returns Double then use auto-unboxing (if you can), though the above code will work for either case
0
 
zzynxSoftware engineerCommented:
Thanx 4 axxepting
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now