public class Whatever {
static double d;
static char c;
static String[] s;
static char[] b;
static double[] dd;
static Whatever w;
static Whatever[] ww;
public static void main(String[] args) {
System.out.println(Whatever.d); //prints out 0.0
System.out.println("hi"+Whatever.c+"hi"); //prints out hi hi
System.out.println(s); //prints out null
System.out.println(b); //null pointer exception!
System.out.println(Whatever.dd);
System.out.println(Whatever.w);
System.out.println(Whatever.ww);
}
}
爲什麼我得到一個空指針異常?爲什麼基元數組字段會輸出空指針異常?
請在內存方面解釋,如果你可以,但是我的記憶有基本的瞭解,所以不要在深入太任。
發佈您的真實代碼。你現在擁有的是不可編譯的。 – Perception
你確定這段代碼編譯得很好,因爲x似乎沒有被聲明爲靜態的嗎? – Dinakar
對不起,在聲明數組時,我忘記了包含關鍵字static。 –