• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

getClass() on Object with null value

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I have a function looking somewhat like this:

calling this with

throws a NullPointerException - nothing wront with that, but the way I figure, 'i' has the type Integer even if it's value is null. When calling 'func' 'value' gets the type Integer, even if the value is null - is this a correct assumption? If 'i' was not null, clazz would of course be 'java.lang.Integer'
So what I would like to archive, is being able to tell what object-type value is, even if it's null - is this possible?
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
throws a NullPointerException - nothing wront with that, but the way I figure, 'i' has the type Integer even if it's value is null. When calling 'func' 'value' gets the type Integer, even if the value is null - is this a correct assumption? If 'i' was not null, clazz would of course be 'java.lang.Integer'

Welcome to JavaRanch S�ren. You must remember that 'i' is not an object but a mere reference to that object. It's class would actually be java.lang.Reference if I am not mistaken. So when you call 'i.getClass()', 'i' is derefernced and now the runtime is attempting to access the underlying object and if that points to an invalid heap address, then naturally a NPE is going to be thrown.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Perhaps you can use i.class to get the Class object instead. I've seen this mentioned here a few times, but I'm not sure if it will work. It may only work on class names rather than reference names.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic