• 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

Surprise! NO NullPointerException while accessing a null object

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,

I'm studying for the SCJP (Java 5.0) and I found the unexpected (but right) behaviour of the following code:


I was expecting:


but I got:


I realized that if I access a static method of a null ref I don't get the ugly NullPointerException.


Filippo
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is because "test3" is a static variable. Compiler will treat it different with normal instance variables.
[ May 07, 2006: Message edited by: wise owen ]
 
Filippo Vitale
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly! I tried adding:

and I got the (now) expected:


So generally we ca say that ((CLASS)null).FIELD) (where FIELD is a static variable of the Class CLASS) doesn't throw a NullPointerException.

Tricky


Filippo
 
Ranch Hand
Posts: 1847
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
a static field is never resolved on an instance but rather on the Class itself.
As the class of the (null)instance is known, it can be resolved. And it will likely be resolved at compiletime rather than runtime, at a point where the compiler either doesn't know or couldn't care less that you're having a null instance.

Many editors (and I think the compiler too if you set certain options) will generate a warning when you write this code along the line of "accessing a static member through an instance".
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also call a static method through the null refernce.
For example:
class A {
static void fun() {
System.out.println("inside function");
}
public static void main(String args[]) {
A obj=null;
obj.fun();
}
}

Output:
========
inside function

Thanks,
Arun Govindan
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic