• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • paul wheaton
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
  • Himai Minh
Bartenders:

Convert null to Number

 
Ranch Hand
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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

 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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."
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 62
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
reply
    Bookmark Topic Watch Topic
  • New Topic