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."); } } }
出力:
例 #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."); } } }
出力:
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:
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 サイトの他の関連記事を参照してください。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

Safe Exam Browser
Safe Exam Browser は、オンライン試験を安全に受験するための安全なブラウザ環境です。このソフトウェアは、あらゆるコンピュータを安全なワークステーションに変えます。あらゆるユーティリティへのアクセスを制御し、学生が無許可のリソースを使用するのを防ぎます。

SublimeText3 Linux 新バージョン
SublimeText3 Linux 最新バージョン

SublimeText3 中国語版
中国語版、とても使いやすい

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)
