Warning: file_exists(): open_basedir restriction in effect. File(/bin/bash) is not within the allowed path(s): (/home/aiflash/:/tmp:/var/tmp:/opt/alt/php82/usr/share/pear/:/dev/urandom:/usr/local/lib/php/:/usr/local/php82/lib/php/) in /home/aiflash/domains/wikippe.e-do-match.com/public_html/includes/shell/CommandFactory.php on line 119
NullPointerException - Wikippe コンテンツにスキップ

NullPointerException

提供: Wikippe

テンプレート:Javadoc:SE(ナル・ポインタ・エクセプション、ヌル-)はプログラミング言語Java における例外の一つ。

解説

この例外はnull値(定義されていない値のこと)の参照型変数を参照しようとした時に発生する。NullPointerExceptionは実行時例外と呼ばれるテンプレート:Javadoc:SE クラスのサブクラスなので、例外処理によるtry-catch節を書かなくてもコンパイルエラーにはならない。

コード例

// NullPointerExceptionSample.java
public class NullPointerExceptionSample {
    public static void main(String[] args) {
        try {
            Integer i = null;

            // ここで NullPointerException がスローされる。
            i.toString(); 

        // ここで NullPointerException がキャッチされる。
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
    }
}

出力例

java.lang.NullPointerException
        at NullPointerExceptionSample.main(NullPointerExceptionSample.java:7)

関連項目

テンプレート:Wikibooks

外部リンク