*
The moose likes Beginning Java and the fly likes NullpointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "NullpointerException" Watch "NullpointerException" New topic
Author

NullpointerException

Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
If I have a few Double variables and I need to add them together. If any of them is null then I get null pointer exception
Doubel d1, d2, d3, d4;
....
Double sum = d1 + d2 + d3 + d4;

what's the easiest way to code to ensure that if any of them is null just skip it or use "0." to replace it ?
Tina Smith
Ranch Hand

Joined: Jul 21, 2011
Posts: 171
    
    5

Use the primitive double type.

A Double is an Object wrapper around the primitive double type. Since it is an Object, it can be null (and like all objects that is the default value).
A double is a primitive type. Since it's a primitive, it's assigned a default value of 0.0.

http://docs.oracle.com/javase/tutorial/java/data/numberclasses.html


Everything is theoretically impossible, until it is done. ~Robert A. Heinlein
Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
I know if it is double then there is no problem. But my prbolem is --- The Double d1, d2,d3, d4 are some variables created by other part and they have reason for it to be wrapper class, in my part I need to sum them up. Actually I can't not even replace it with 0.0 if it is null. If it is null I should just skip that one, if all of them are null then I should return null. If some of them are not null I should sum those non null values up.

Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Raj Ohadi wrote:

From what you say, your only option is to test each one for null. Additionally, you'll need to keep track of whether you've had any non-null values so far, so that you know whether to return null or not.
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

I would write it something like this:
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Aditya Jha wrote:I would write it something like this:


Aditya, please remember that this site is NotACodeMill(⇐click) and that, as it says on the topics page, "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers."
Alex Armenteros
Ranch Hand

Joined: May 05, 2010
Posts: 69
Using tertiary operators

Double sum=(Double.valueOf(d1)==null?0.0:d1)+(Double.valueOf(d2)==null?0.0:d2)+(Double.valueOf(d3)==null?0.0:d3)+(Double.valueOf(d4)==null?0.0:d4);

This way dx can be a Double object or a double primitive or even a int,float,byte,etc and it will work as intended
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Alex Armenteros wrote:Using tertiary operators

Double sum=(Double.valueOf(d1)==null?0.0:d1)+(Double.valueOf(d2)==null?0.0:d2)+(Double.valueOf(d3)==null?0.0:d3)+(Double.valueOf(d4)==null?0.0:d4);


Alex, please remember that this site is NotACodeMill(⇐click) and that, as it says on the topics page, "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers."

Also, that approach won't work. It will throw NPE an the auto-unbox of a null d1, etc., and Double.valueOf() never returns null.

Also there's no reason to call valueOf since we already have Doubles, not doubles or Strings.

Also, it's really hard to read.

Also, it's "ternary", not "tertiary."

This way dx can be a Double object or a double primitive or even a int,float,byte,etc and it will work as intended


No, definitely not.
Raj Ohadi
Ranch Hand

Joined: Jun 30, 2006
Posts: 316
Thanks. I like Aditya Jha 's approach. Clean and nice.
Aditya Jha
Ranch Hand

Joined: Aug 25, 2003
Posts: 227

Jeff Verdegan wrote:
Aditya Jha wrote:I would write it something like this:


Aditya, please remember that this site is NotACodeMill(⇐click) and that, as it says on the topics page, "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers."

Point taken, Jeff!

To be honest, I have always paid attention to this point, and have tried to put a hint to the solution in words as far as possible. However, for some reason, this question (and my solution) seemed like a case where writing the solution in words or code will not make much of a difference. In other words, I wouldn't be talking about the strategy to use, rather the way to code itself (use an array etc.), which will be almost equivalent to a code-piece.

But in the spirit of your suggestion, I completely agree with you.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Aditya Jha wrote:
Jeff Verdegan wrote:
Aditya Jha wrote:I would write it something like this:


Aditya, please remember that this site is NotACodeMill(⇐click) and that, as it says on the topics page, "We're all here to learn, so when responding to others, please focus on helping them discover their own solutions, instead of simply providing answers."

Point taken, Jeff!

To be honest, I have always paid attention to this point, and have tried to put a hint to the solution in words as far as possible. However, for some reason, this question (and my solution) seemed like a case where writing the solution in words or code will not make much of a difference.


I understand. I do the same thing sometimes too. If the OP has shown effort, and is reasonably close, sometimes there's no harm in just showing the small code change needed to fix things, and sometimes trying to explain it in prose just leads to confusion. Where to draw that line is subjective, of course, but in this case, I do think you gave a little too much code a little prematurely.

I'm glad you've at least got the right attitude about it though!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: NullpointerException
 
Similar Threads
Logical error in creating a HashMap
Double / Float Doubt.
trouble with precision adding floats together
double call by reference?
Time format in Java