上級 › オートボクシングで起きる NullPointerException › パターン4
オートボクシングで起きる NullPointerException
Integerはnullを持てますが、演算時にintへ開封されます。
1 public class Main { 2 public static void main(String[] args) { 3 Integer a = null; 4 boolean condition = false; 5 int result = condition ? 1 : ____________; ^ 6 System.out.println(result); 7 } 8 }
Exception in thread "main" java.lang.NullPointerExceptionexited with code 1
空欄を埋めてビルドを通す
三項演算子の結果をintで受けると、選ばれた側のIntegerがnullなら自動開封の瞬間に例外になります。
次の問題