「Java入門」java.lang.NullPointerExceptionのサンプル

サンプルコード

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
package com.example;
public class StringDemo {
@SuppressWarnings("null")
public static void main(String[] args) {
String strA = null;
try {
if (strA.length() > 0) {
System.out.println("test");
}
} catch (Exception e) {
System.out.println("エラーメッセージ:\n" + e);
}
}
}
package com.example; public class StringDemo { @SuppressWarnings("null") public static void main(String[] args) { String strA = null; try { if (strA.length() > 0) { System.out.println("test"); } } catch (Exception e) { System.out.println("エラーメッセージ:\n" + e); } } }
package com.example;

public class StringDemo {

      @SuppressWarnings("null")
      public static void main(String[] args) {

            String strA = null;
            try {
                  if (strA.length() > 0) {
                        System.out.println("test");
                  }
            } catch (Exception e) {
                  System.out.println("エラーメッセージ:\n" + e);
            }

      }

}

実行結果

エラーメッセージ:
java.lang.NullPointerException

Java

Posted by arkgame