Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NullpointerException

 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 208
9
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Raj Ohadi
Ranch Hand
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would write it something like this:
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 316
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I like Aditya Jha 's approach. Clean and nice.
 
Aditya Jha
Ranch Hand
Posts: 227
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic