java.lang.NullPointerException(NPE)に気づきましたか? JavaでランタイムNPEを回避するための8つのベストプラクティス
公開: 2020-10-07
JavaおよびJavaでのNullPointerExceptionの回避JavaでNullPointerExceptionを回避するためのヒントとベストプラクティス。
Java開発者として、あなたは1日目からNull Pointer Exception(NPE)に直面したに違いありません。 ほとんどの場合、NPE例外は、同じ原因を特定する明確なスタックトレースを示しますが、数百のクラスがある大規模なエンタープライズレベルのアプリケーションの場合、実際の根本原因を見つけるのは悪夢になります。
Null Pointer Exception(NPE)とは何ですか?
NullPointerException (NPE)
は、オブジェクトを参照しているかのように、メモリ内の場所がない(null)参照を使用しようとしたときに発生する例外です。
null参照でメソッドを呼び出すか、 null
参照のフィールドにアクセスしようとすると、NPEがトリガーされます。 これが最も一般的な原因です。
JavaDocによると、NPEの主な原因は次のとおりです。
-
Throwable
値であるかのようにnull
をスローします。 -
null
オブジェクトのインスタンスメソッドを呼び出します。 -
null
オブジェクトのフィールドへのアクセスまたは変更。 -
null
の長さを配列であるかのように取ります。 -
null
のスロットにアクセスするか、配列であるかのように変更します。
さて、本当の問題は、実行時にjava.lang.NullPointerExceptionを回避する方法ですか? このチュートリアルでは、実行時にNPEを作成するいくつかの例と、これを解決するために実行する必要のある手順を見ていきます。
実行時1日にNPEを作成しましょう。 以下の例CrunchifyNullPointerExceptionTips.java
を見てください
NPEを3つの異なる方法で作成します
- nullオブジェクトにアクセスしようとすると、NPEがスローされます
- null文字列を変換しようとすると、NPEがスローされます
- クラスの初期化中にnullオブジェクトにアクセスしようとすると、NPEがスローされます
クラスCrunchifyNullPointerExceptionTips.javaを作成します
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
package crunchify . com . tutorial ; /** * @author Crunchify.com * Have you Noticed java.lang.NullPointerException (NPE)? 8 Best practices to avoid runtime NPE in Java */ public class CrunchifyNullPointerExceptionTips { public static void main ( String [ ] args ) { try { // Example 1: NPE will be thrown if you are trying to access null Object CrunchifyNPE1 ( ) ; } catch ( NullPointerException crunchifyNPE1 ) { System . out . println ( "Exception in CrunchifyNPE1()" ) ; crunchifyNPE1 . printStackTrace ( ) ; } try { // Example 2: NPE will be thrown if you are trying to convert null String CrunchifyNPE2 ( ) ; } catch ( NullPointerException crunchifyNPE2 ) { System . out . println ( "\nException in CrunchifyNPE2()" ) ; // printStackTrace(): Prints this throwable and its backtrace to the standard error stream. // This method prints a stack trace for this Throwable object on the error output stream that is the value of the field System.err. // The first line of output contains the result of the toString() method for this object. // Remaining lines represent data previously recorded by the method fillInStackTrace(). crunchifyNPE2 . printStackTrace ( ) ; } try { // Example 3: NPE will be thrown if you are trying to access null Object during Class Initialization CrunchifyNPETest npe = new CrunchifyNPETest ( ) ; npe . getName ( ) ; // NullPointerException: Thrown when an application attempts to use null in a case where an object is required. These include: // - Calling the instance method of a null object. // - Accessing or modifying the field of a null object. // - Taking the length of null as if it were an array. // - Accessing or modifying the slots of null as if it were an array. // - Throwing null as if it were a Throwable value. } catch ( NullPointerException crunchifyNPE3 ) { System . out . println ( "\n Exception in CrunchifyNPETest()" ) ; crunchifyNPE3 . printStackTrace ( ) ; } } private static void CrunchifyNPE1 ( ) { Object crunchifyObj = null ; // hasCode(): Returns a hash code value for the object. // This method is supported for the benefit of hash tables such as those provided by java.util.HashMap. crunchifyObj . hashCode ( ) ; } private static void CrunchifyNPE2 ( ) { String crunchifyString ; crunchifyString = "https://crunchify.com" ; // The line 40 declares a variable named "crunchifyString", but, it does not contain a primitive value. Instead it contains a pointer (because the type is String // which is a reference type). Since you did not say as yet what to point to Java sets it to null, meaning "I am pointing at nothing". // In line 41, the new keyword is used to instantiate (or create) an object of type String and the pointer variable "crunchifyString" is assigned this // object. You can now reference the object using the dereferencing operator . (a dot). System . out . println ( "\nvalue: " + crunchifyString . toString ( ) + ", length: " + crunchifyString . length ( ) ) ; System . out . println ( "No NPE exception on line 51" ) ; // Now Let's create NPE String crunchifyString2 = null ; System . out . println ( crunchifyString2 . toString ( ) ) ; } } class CrunchifyNPETest { private String crunchifyName ; public void setName ( String name ) { this . crunchifyName = name ; } public void getName ( ) { printName ( crunchifyName ) ; } private void printName ( String s ) { System . out . println ( s + " (" + s . length ( ) + ")" ) ; } } |
結果:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
Exception in CrunchifyNPE1 ( ) java . lang . NullPointerException : Cannot invoke "Object.hashCode()" because "crunchifyObj" is null at crunchify . com . tutorial . CrunchifyNullPointerExceptionTips . CrunchifyNPE1 ( CrunchifyNullPointerExceptionTips . java : 59 ) at crunchify . com . tutorial . CrunchifyNullPointerExceptionTips . main ( CrunchifyNullPointerExceptionTips . java : 14 ) value : https : //crunchify.com, length: 21 No NPE exception on line 51 Exception in CrunchifyNPE2 ( ) Exception in CrunchifyNPETest ( ) java . lang . NullPointerException : Cannot invoke "String.toString()" because "crunchifyString2" is null at crunchify . com . tutorial . CrunchifyNullPointerExceptionTips . CrunchifyNPE2 ( CrunchifyNullPointerExceptionTips . java : 75 ) at crunchify . com . tutorial . CrunchifyNullPointerExceptionTips . main ( CrunchifyNullPointerExceptionTips . java : 23 ) java . lang . NullPointerException : Cannot invoke "String.length()" because "s" is null at crunchify . com . tutorial . CrunchifyNPETest . printName ( CrunchifyNullPointerExceptionTips . java : 92 ) at crunchify . com . tutorial . CrunchifyNPETest . getName ( CrunchifyNullPointerExceptionTips . java : 88 ) at crunchify . com . tutorial . CrunchifyNullPointerExceptionTips . main ( CrunchifyNullPointerExceptionTips . java : 38 ) Process finished with exit code 0 |
実行時にNullPointerExceptionを回避するために使用できるヒントとコツはいくつかあります。 見てみましょう。

ヒント1:
Eclipse / IntelliJ IDEは、ワークスペースにNPEを表示しようとします。 開発中にのみコードを修正してください。

ヒント2:
オブジェクトsNullorEmpty
crunchifyI
()
チェックを追加します。 これをCrunchifyNullPointerExceptionTips.java
に追加します
1 2 3 4 5 6 7 8 |
public static boolean crunchifyIsNullOrEmpty ( String crunchifyStr ) { if ( crunchifyStr == null ) return true ; else if ( crunchifyStr . trim ( ) . equals ( "" ) ) return true ; else return false ; } |
上記のJavaプログラムでは、55行目と56行目がこれに置き換えられます。
1 2 3 4 5 6 |
String crunchifyString2 = null ; if ( ! crunchifyIsNullOrEmpty ( crunchifyString2 ) ) { System . out . println ( crunchifyString2 . toString ( ) ) ; } else { System . out . println ( "crunchifyString2 is null" ) ; } |
ヒント3:
Trim()操作後、Stringがnull
空かどうかを確認します。
1 2 3 |
public static boolean isNullOrEmptyAfterTrim ( String crunchifyStr ) { return ( crunchifyStr == null | | crunchifyStr . trim ( ) . length ( ) == 0 ) ; } |
ヒント4:
ランタイムプロセスが中断されないようにするには、常にTry Catch block
を使用してください。
1 2 3 4 5 |
try { CrunchifyNPE1 ( ) ; } catch ( NullPointerException npe ) { System . out . println ( "Exception in CrunchifyNPE1()" + npe ) ; } |
ヒント5:
ジェネリックスの処理が優れているため、 Collections.emptyList()
が推奨されます。
ヒント6:
Java Assertions
を使用する
アサーションは、コードに関する仮定をテストできるようにするステートメントです。 たとえば、システムで名前を返すメソッドを作成する場合、Stringがnullの場合にotherNameを返すことを表明できます。
アサーションの基本的な使用法は次のとおりです。
1 2 3 4 5 6 7 8 |
assert < Expression > ; // or another usage is assert < Expression1 > : < Expression2 > ; // in our program add line below. private void printName ( String s ) { assert ( s ! = null ) : "Name must be not null" ; System . out . println ( s + " (" + s . length ( ) + ")" ) ; } |
ただし、落とし穴があります。アサーションは実稼働環境では使用できないため、ビジネスロジックでアサーションを使用しないでください。
ヒント7:
containsKey()
、 containsValue()
、 contains()
チェックを使用してみてください。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com . crunchify . tutorial ; import java . util . * ; /** * @author Crunchify.com * */ public class CrunchifyContainsKeyExample { public static void main ( String args [ ] ) { HashMap < Integer , String > crunchifyMap = new HashMap < Integer , String > ( ) ; // populate hash map crunchifyMap . put ( 1 , "Crunchify" ) ; crunchifyMap . put ( 2 , "wordpress" ) ; crunchifyMap . put ( 3 , "java tutorials" ) ; // check existence of key 4 if ( crunchifyMap . containsKey ( 4 ) ) { System . out . println ( "Check if key 2 exists: " + crunchifyMap . get ( 4 ) ) ; } else { System . out . println ( "NPE for value 4 avoided" ) ; } } } |
ヒント8:
結論として、実行時に本番環境でデバッグするのではなく、開発中にNPEを処理することをお勧めします。 Spring Frameworkアノテーション、 Factory Pattern
、ヌルオブジェクトパターンなどを使用して利用できるヒントやコツは他にもたくさんあります。しかし、ここではこれを短くします。
これについては、おそらく1週間以内に新しいチュートリアルを公開する予定です。 乞うご期待。
このチュートリアルに何かを追加したい場合、または問題を見つけた場合は、チャイムを鳴らしてコメントを入力してください。