java - Why do I get null pointer exception in this code? -
public class test { integer i; int j; public static void main ( string [] args ) { test t = new test (); t.go(); } public void go() { j=i; system.out.println(j); system.out.println(i); } }
output : exception in thread "main" java.lang.nullpointerexception @ test.go(test.java:12) @ test.main(test.java:8)
that's not error. runtime nullpointerexception because you're unboxing null reference (i
) primitive (j
). see jls §5.1.8.
the reason i
null instance fields initialized 0, null, or false default.
Comments
Post a Comment