File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Convert null to Number Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Convert null to Number" Watch "Convert null to Number" New topic
Author

Convert null to Number

Gabriel Beres
Ranch Hand

Joined: Sep 09, 2006
Posts: 61
Hi!

I want to ensure that if a Double or Integer is null, it is converted to 0.0 and 0, thus preventing nullpointer.

I'm thinking about a generic method that takes a Number, and checks if it is Double or Integer with instanceof.
Then it checks for null and if it is, it returns new Double(0.0) or new Integer(0). The method would have a return type Number in this case.

I wonder if such method already exists somewhere in java or apache commons. So far i could not find anything like this.

Thanks

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11229
    
  16

ok...first, remember that in java, you work with references, not the objects themselves. so, you have a reference that is set to null.

It's like saying "how many eggs are in the carton?" the answer can be zero, 1, 1000...but if the reference is null, then the answer is "i don't even have a carton to look in.

About the only thing you can do is something like



That says "If d2 doesn't have an egg carton, put one there with no eggs in it."


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

If it's null, then instanceof will always be false. There's no way to tell if a null was supposed to be an Integer or a Double or a Date or a JPanel, unless you're in the body of a method that has that type (or a subtype) as an arg. There is no such thing as a null Integer or a null Double.

If you want to distinguish a null that would have been an Integer from a null that could have been a Double, you need either context or metadata. The null itself is not enough.
Gabriel Beres
Ranch Hand

Joined: Sep 09, 2006
Posts: 61
Jeff Verdegan wrote:If it's null, then instanceof will always be false. There's no way to tell if a null was supposed to be an Integer or a Double or a Date or a JPanel, unless you're in the body of a method that has that type (or a subtype) as an arg. There is no such thing as a null Integer or a null Double.

If you want to distinguish a null that would have been an Integer from a null that could have been a Double, you need either context or metadata. The null itself is not enough.


Thanks this information was usefull. I completely forgot NULL has no type. Let's pretend i pass the type to my generic method too. The question remains the same. Shall i write this method, or exists somewhere?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Gabriel Beres wrote:I completely forgot NULL has no type.


It's null, not NULL, and its type is the null type

Let's pretend i pass the type to my generic method too. The question remains the same. Shall i write this method, or exists somewhere?


A method to interpret null as zero? You have to write that yourself.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert null to Number