中級 › NullPointerException の原因と直し方 › パターン1
NullPointerException の原因と直し方
nullに対する操作で発生します。
1 public class Main { 2 public static void main(String[] args) { 3 String s = ____; ^ 4 System.out.println(s.length()); 5 } 6 }
Exception in thread "main" java.lang.NullPointerException:
Cannot invoke "String.length()" because "s" is nullexited with code 1
空欄を埋めてビルドを通す
nullは実体がない状態です。空文字""は長さ0のStringという実体なので、メソッドを呼べます。
次の問題