You first have to understand that the code you have is actually compiled to something a little different.
[s_delima@ml-l-sotiriosd bin]$ /usr/java/latest/bin/javap -c Test.class
Compiled from "Test.java"
public class Test {
...
public static void main(java.lang.String[]) throws java.io.IOException;
Code:
0: aconst_null
1: astore_1
2: aload_1
3: invokevirtual #19 // Method java/lang/Integer.intValue:()I
6: istore_2
7: return
}
You will notice that there is an invocation of java/lang/Integer.intValue()
. Since your Integer
variable is referencing null
, you will get a NullPointerException
when the invocation tries to dereference it.