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 サイトの他の関連記事を参照してください。

JavaremainsagoodlanguagedueToitscontinuousevolution androbustecosystem.1)lambdaexpressionsenhancecodereadability andenableFunctionalprogramming.2)streamsalowsolowsolfisitydataprocessing、特に特にlagedatasets.3)硬化系系統系系統系系統系系統

Javaisgreatduetoitsplatformindependence、robustoopsupport、extensiveLibraries、andstrongCommunity.1)PlatformentepenteviajvMallowsCodeTorunonVariousPlatforms.2)oopeatureSlikeEncapsulation、遺伝、およびポリモ系系統型皮下皮質皮下Rich

Javaの5つの主要な特徴は、多型、Lambda Expressions、StreamSapi、ジェネリック、例外処理です。 1。多型により、さまざまなクラスのオブジェクトを一般的なベースクラスのオブジェクトとして使用できます。 2。Lambda式は、コードをより簡潔にし、特にコレクションやストリームの処理に適しています。 3.ストリームサピは、大規模なデータセットを効率的に処理し、宣言操作をサポートします。 4.ジェネリックは、タイプの安全性と再利用性を提供し、型刻印中にタイプエラーがキャッチされます。 5.例外処理は、エラーをエレガントに処理し、信頼できるソフトウェアを作成するのに役立ちます。

java'stoputuressificlynificlytallysperformanceandscalability.1)object-oriented-principleslikepolymorphismenabledscalablecode.2)garbagecolectionAutomateMemorymarymanagemenateButcancausElatenceSuses.3)

JVMのコアコンポーネントには、クラスローダー、runtimedataarea、executionEngineが含まれます。 1)クラスローダーは、クラスとインターフェイスの読み込み、リンク、初期化を担当します。 2)runtimedataareaには、Methodarea、Heap、Stack、Pcregister、Nativemethodstackが含まれています。 3)ExecutionEngineは、Bytecodeの実行と最適化を担当する通訳、JitCompiler、GarbageCollectorで構成されています。

Java'ssafetyandsecurityarebolteredby:1)stronttyping、whathspreventype-relatederrors; 2)自動メモリ管理viagarbagececollection、3)サンドボクシング、分離コードフロムシェシシステム;

Javaoffersseveralkeyfeaturesthatenhancecodingskills:1)Object-orientedprogramingallowsmodelingreal-worldentities、explifiedBypolymorphism.2)例外ハンドリングプロビッドログスロルマニネーション

jvmisacrucialcomponentthaturunsjavacodebytrantingintiTomachine特異的インストラクション、パフォーマンス、セキュリティ、およびポータビリティに影響を与えます


ホットAIツール

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

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

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

WebStorm Mac版
便利なJavaScript開発ツール

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

mPDF
mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

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

PhpStorm Mac バージョン
最新(2018.2.1)のプロフェッショナル向けPHP統合開発ツール
