Domanda

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 java.lang.NullPointerException "principale"     a Test.go (Test.java:12)     a Test.main (Test.java:8)

È stato utile?

Soluzione

Questo non è, ovviamente, l'errore. Si otterrà un NullPointerException runtime perché si sta unboxing un riferimento null (i) in un primitivo (j). Vedere JLS §5.1.8 .

Il motivo i è nulla è che esempio campi sono inizializzati a 0, null o false di default.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top