Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes NullPointerException Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "NullPointerException" Watch "NullPointerException" New topic
Author

NullPointerException

Anuji Philip
Ranch Hand

Joined: Feb 25, 2002
Posts: 46
public class General{
static int i;
public static void main(String argv[]){
General g = null;
System.out.println( g.i);
}
}
why no null pointer exception here?My idea was that referring g as General.i is the easy way.But if the object is created, it go through
object.What internal mechanism going on here?
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
The key here is that you're accessing a static member. Due to that, the JVM really only takes the type of g (that being General) and looks for General.i. Here's an exerpt from the JLS, §15.11.1 Field Access Using a Primary:

The type of the Primary must be a reference type T, or a compile-time error occurs. The meaning of the field access expression is determined as follows:
- If the identifier names several accessible member fields of type T, then the field access is ambiguous and a compile-time error occurs.
- If the identifier does not name an accessible member field of type T, then the field access is undefined and a compile-time error occurs.
- Otherwise, the identifier names a single accessible member field of type T and the type of the field access expression is the declared type of the field. At run time, the result of the field access expression is computed as follows:
- If the field is static:
- If the field is final, then the result is the value of the specified class variable in the class or interface that is the type of the Primary expression.
- If the field is not final, then the result is a variable, namely, the specified class variable in the class that is the type of the Primary expression.
- If the field is not static:
- If the value of the Primary is null, then a NullPointerException is thrown.
- If the field is final, then the result is the value of the specified instance variable in the object referenced by the value of the Primary.
- If the field is not final, then the result is a variable, namely, the specified instance variable in the object referenced by the value of the Primary.
Note, specifically, that only the type of the Primary expression, not the class of the actual object referred to at run time, is used in determining which field to use.

You have to read between the lines a little bit to get what you need out of that, but the JLS goes on to give the following example:

and has these comments about it:

It compiles, executes, and prints:
Mount Chocorua
Even though the result of favorite() is null, a NullPointerException is not thrown. That "Mount " is printed demonstrates that the Primary expression is indeed fully evaluated at run time, despite the fact that only its type, not its value, is used to determine which field to access (because the field mountain is static).

I know this is a bit confusing but, hopefully, from what's given in the JLS, you'll understand why this is. Notice that, had i been an instance variable, rather than a static variable, you would have gotten a NullPointerException. If you have any more questions about this, just let me know.
I hope that helps,
Corey


SCJP Tipline, etc.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: NullPointerException