你注意到 java.lang.NullPointerException (NPE) 了嗎? 在 Java 中避免運行時 NPE 的 8 個最佳實踐
已發表: 2020-10-07
避免 Java 中的 Null 指針異常和 Java 提示和最佳實踐以避免 Java 中的 NullPointerException。
作為一名 Java 開發人員,我相信您從第一天開始一定遇到過空指針異常 (NPE)。 在大多數情況下,NPE 異常會顯示清晰的堆棧跟踪,它指出了相同的根本原因,但在大型企業級應用程序的情況下,您有數百個類,找出真正的根本原因就變成了噩夢。
什麼是空指針異常 (NPE)?
NullPointerException (NPE)
是當您嘗試使用指向內存中沒有位置 (null) 的引用時發生的異常,就好像它正在引用一個對像一樣。
在null
引用上調用方法或嘗試訪問空引用的字段將觸發 NPE。 這是最常見的原因。
根據 JavaDoc,以下是 NPE 的主要原因:
- 把
null
當作一個Throwable
值來拋出。 - 調用
null
對象的實例方法。 - 訪問或修改
null
對象的字段。 - 將
null
的長度視為一個數組。 - 訪問或修改
null
的槽,就像它是一個數組一樣。
現在真正的問題是如何在運行時避免 java.lang.NullPointerException? 在本教程中,我們將查看一些在運行時創建 NPE 的示例以及我們需要執行的步驟以解決此問題。
讓我們在運行時創建 NPE 1。 看看下面的例子CrunchifyNullPointerExceptionTips.java
我們將以 3 種不同的方式創建 NPE
- 如果您嘗試訪問 null 對象,將拋出 NPE
- 如果您嘗試轉換空字符串,將拋出 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:
在對對象進行操作之前添加crunchifyI
sNullorEmpty
()
檢查。 將此添加到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 為空,則返回的 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 Annotation、 Factory Pattern
、Null Object Pattern 等還有許多其他提示和技巧可用。但我現在將縮短這一點。
可能會在一周內發布關於這個的新教程。 敬請關注。
您想在本教程中添加任何內容或發現問題,請加入並提供您的意見。