Code Fix

中級NullPointerException の原因と直し方 › パターン3

NullPointerException の原因と直し方

nullに対する操作で発生します。

 1  public class Main {
 2      static String find(int n) {
 3          if (n > 0) return "found";
 4          return ____;
                     ^
 5      }
 6      public static void main(String[] args) {
 7          System.out.println(find(-1).length());
 8      }
 9  }
Exception in thread "main" java.lang.NullPointerExceptionexited with code 1

空欄を埋めてビルドを通す

広告
広告スロット(未設定)

NullPointerException の原因と直し方

発生箇所の特定方法と、そもそもnullを持ち回さない設計を扱います。

このエラーの解説と類題をまとめて見る →

広告
広告スロット(未設定)