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
AndyC1000Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSr. Software 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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
zzynxSr. Software engineerCommented:
Thanx 4 axxepting
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.