ホームページ  >  記事  >  Java  >  Java NullPointerException

Java NullPointerException

王林
王林オリジナル
2024-08-30 16:13:03838ブラウズ

NullPointer Exception は、プログラマが頻繁に発生する Java の例外です。これは、オブジェクトのインスタンス化が適切でない場合に発生する実行時例外です。オブジェクトは null 値を含むものとして宣言されています。 NULL ポインター例外は、NULL 値を含む参照を持つオブジェクトを呼び出そうとしていることを単に意味します。心に留めておくべき最も重要なことは、Java 言語はポインターの概念をサポートしていないため、null ポインター例外はポインターとは関係がないということです。むしろ、オブジェクト参照に関連付けられています。

無料ソフトウェア開発コースを始めましょう

Web 開発、プログラミング言語、ソフトウェア テスト、その他

構文:

Null ポインター例外を実行時エラーとして指定するための特定の構文はありません。これは自動的に生成され、使用できるように表示されます。

キャッチ例外を宣言して、NullPointer 例外のポイントとデバッグにスローできます。 NullPointerException のデバッグは非常に面倒です。実行時にオブジェクトのインスタンス化と参照宣言が適切であることを確認するために必要です。

try {
Object obj1 = new Object(); // Instantiate a new object for the class as reference
Obj1=null; //Provide null value to the object created
Logging.log(String.format(“get the object value”, obj1.object1for reference));
}
catch(java.lang.NullPointerException exception)
{
Logging.log(exception);
}
catch(Throwable exception)
{
Logging.log(exception,false);
}

Java で NullPointerException はどのように動作しますか?

Java の

NullPointerException は、開発者にとって決して望ましいエラーではない例外です。例外は実行時に毎回発生するため、例外を見つけるのは非常に困難です。したがって、そのような例外を見つけるのは面倒な作業です。プログラマが NullPointerException を見つけるには何時間もかかります。

この問題は、オブジェクトが他の目的で必要なときに、null で定義されたオブジェクトをどこかに割り当てようとする異常な試みが行われたときに発生します。 NullPointerException を含むすべての Java エラーは、次のような特定の理由で発生します。

  • java.lang.the throwable インターフェースが宣言されるたびに、考えられるすべての Java エラーがスローされ、継承されたクラスによってさらに拡張されます。
  • lang.Exception は java.lang.throwable.
  • から継承されます。
  • lang.throwable クラスは java.lang.Exception.
  • を拡張します。
  • この継承クラスが原因で Java.lang.RuntimeException も発生します。
  • 最終的に、java.lang.NullPointerException は java.lang.RuntimeException クラスから継承されます。

NullPointerException が java.lang.RuntimeException から継承されることが明確に述べられているように、この問題は、アプリケーションのコンパイル時および実行時にアプリケーションの実行があるたびに発生します。また、インスタンス化時にフィールド、メソッド、またはオブジェクトの不正な参照に直接アクセスしようとした場合は、必ず java.lang.NullPointerException をスローする必要があります。ロガーを追加してさらにダミー オブジェクトを作成し、null が割り当てられたオブジェクトのメソッドのインスタンスを呼び出すと、コードを適切にデバッグし、NullPointerException の根本原因と、エラーと例外を指す行を見つけるのにも役立ちます。したがって、これは、特定の行で発生する Java エラーを理解するためのトラブルシューティングとデバッグの非常に一般的な方法です。

Java NullPointerException のコンストラクター

NullPointerException のメソッドで定義および宣言された特定のコンストラクターが次のとおりです。

1. NullPointerException()

このコンストラクターは、詳細なメッセージや説明のない NullPointerException を構築するために使用されます。これは、要件に従ってあまり推奨されない、空または null の例外と見なすことができます。

2. NullPointerException(String s)

このコンストラクターは、指定された場所で発生する詳細メッセージの一部を含み、Null ポインター例外の構築時に使用できる引数を含むため、NullPointerException() とは矛盾する動作をします。引数 String s は、詳細メッセージを含む null ポインター例外を作成する役割を果たします。

Java NullPointerException の実装例

以下は Java NullPointerException の例です:

例 #1

このプログラムは、実行時に無効なメソッドの呼び出しが行われ、結果として NullPointerException が発生することを示すために使用されますが、これは必須ではありません。

コード:

public class Null_Pointer_Excptn {
public static void main(String[] args) {
String strng = null;
try
{
if (strng.equals("monkey"))
System.out.println("Let us take a value which needs to be similar");
else
System.out.println("Otherwise it will not take a similar and equal value.");
}
catch(NullPointerException e)
{
System.out.println("It is a need to catch the null pointer exception.");
}
}
}

出力:

Java NullPointerException

例 #2

このプログラムは、従来のメソッドではないため NullPointerException の作成を回避する Java プログラムを示しています。

コード:

public class Null_Pointer_Excptn_Avoid {
public static void main(String[] args) {
String strng2 = null;
try
{
if ("avoid_null".equals(strng2))
System.out.println("Coming out to be equal");
else
System.out.println("It is not at all coming out to be equal");
}
catch(NullPointerException e)
{
System.out.println("Catch the null pointer Exception to get a clarification.");
}
}
}

出力:

Java NullPointerException

Example #3

This program illustrates that the NullPointerException can be avoided using the proper object verification before initialization.

Code:

public class Good_Null_Pntr_Excpn {
public static void main(String[] args) {
String store = "Learning";
try
{
System.out.println(getLength(store));
}
catch(IllegalArgumentException e)
{
System.out.println("Need to catch the definition of illegalArgument.");
}
store = "Educba";
try
{
System.out.println(getLength(store));
}
catch(IllegalArgumentException e)
{
System.out.println("Need to catch the definition of illegalArgument.");
}
store = null;
try
{
System.out.println(getLength(store));
}
catch(IllegalArgumentException e)
{
System.out.println("Need to catch the definition of illegalArgument.");
}
}
public static int getLength(String store)
{
if (store == null)
throw new IllegalArgumentException("Need to catch the definition of illegalArgument.");
return store.length();
}
}

Output:

Java NullPointerException

How to Avoid NullPointerException?

As it is not a good practice to get NullPointerException as it makes the entire codebase cumbersome and consumes time for the programmers to figure out the root cause of the NullPointerException.

Therefore, it is very much needed to avoid these exceptions which can make sure using the following ways:

  • By comparing a string with the literals.
  • By keeping a check on the passed arguments or parameters being passed from the method.
  • By making use of the ternary operator.

Conclusion

Java NullPointerException is an exception which is not a good option and is recommended not to occur as it consumes a lot of time. Debugging and troubleshooting become difficult; therefore, it must keep in mind that before the initialization of the object, the reference of the object must be proper. Therefore, the reference of the object’s null value should be proper, and then it can be avoided.

以上がJava NullPointerExceptionの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。