網(wǎng)站正在建設中 頁面渠道推廣
參考鏈接:
-
java中什么是空指針異常以及為什么會產(chǎn)生空指針異常天上的云川的博客-CSDN博客什么是java空指針
-
java中容易產(chǎn)生空指針異常:NullPointerException的場景火龍映天的博客-CSDN博客java怎么制造空指針異常
-
java空指針異常是什么、怎么發(fā)生、如何處理僵尸道長毛小方的博客-CSDN博客空指針異常是什么
1. 名詞解釋
指針:指針中存放的是內(nèi)存地址。
空:null
空指針:指針不指向任何內(nèi)存地址(沒有初始化分配內(nèi)存,獲得引用)
空指針異常:一個指針不指向任何內(nèi)存地址,但仍被調(diào)用了。
打開 NullPointerException源碼,開頭就寫明出現(xiàn)NullPointerException的原因:
-
Invoking a method from a null object:調(diào)用空對象的方法
-
obj.method() // obj對象不存在
-
-
Accessing or modifying a null object’s field:獲取或修改空對象的字段
-
obj.setName("cjn") // obj對象不存在
-
-
Taking the length of null, as if it were an array:獲取一個空數(shù)組的長度
-
array.length // array為null
-
-
Accessing or modifying the slots of null object, as if it were an array:獲取或者修改空集合的一個位置上的內(nèi)容
-
arr[100]=100; // array為null
-
-
Throwing null, as if it were a Throwable value:將null視為Throwable值
-
When you try to synchronize over a null object:當你嘗試同步一個空對象
2. 代碼測試
測試代碼如下:
?package exception;?import cn.hutool.core.collection.CollUtil;import cn.hutool.core.util.StrUtil;import compare.User;?import java.util.Hashtable;import java.util.List;?/*** @ClassName NullPointerExceptionTest* @Description 產(chǎn)生空指針異常的原因:在null對象上調(diào)用方法或者獲取屬性* @Author Jiangnan Cui* @Date 2023/2/13 21:05* @Version 1.0*/public class NullPointerExceptionTest {public static void main(String[] args) {stringNullPointerException();collectionNullPointerException();packageClassNullPointerException(); ?}?/*** 測試字符串產(chǎn)生的空指針異常*/public static void stringNullPointerException(){System.out.println("測試字符串產(chǎn)生的空指針異常");String str = null;// 1. 字符串內(nèi)容為null,調(diào)用字符串相關方法時會產(chǎn)生空指針異常// ? ? ? if(!str.isEmpty()){// ? ? ? ? ? System.out.println("str = " + str);// ? ? ? }// 優(yōu)化:先判斷不為null,滿足后再調(diào)用其所屬方法if(str != null && !str.isEmpty()){System.out.println("str = " + str);}?// 2. 字符串內(nèi)容為null,進行字符串內(nèi)容比較時會產(chǎn)生空指針異常// ? ? ? if(str.equals("test")){// ? ? ? ? ? System.out.println("str = " + str);// ? ? ? }// 優(yōu)化:// a. 將不為null的字符串內(nèi)容放在前方if("test".equals(str)){System.out.println("str = " + str);}// 2. 使用StrUtil.equals()方法比較,此時str放在前后均可以// str放在前面if(StrUtil.equals(str, "test")){System.out.println("str = " + str);}// str放在后面if(StrUtil.equals("test", str)){System.out.println("str = " + str);}}?/*** 測試包裝類自動拆箱時產(chǎn)生的空指針異常*/public static void packageClassNullPointerException(){Integer integer = null;// ? ? ? int number = integer;// ? ? ? System.out.println("number = " + number);// 優(yōu)化:先判空,再賦值int number = 0;if(integer != null){number = integer;}System.out.println("number = " + number);}?/*** 測試集合調(diào)用時產(chǎn)生的空指針異常*/public static void collectionNullPointerException(){// 1. 集合為空時,調(diào)用集合相關方法會產(chǎn)生空指針異常List<String> list = null;// ? ? ? if(list.isEmpty()){// ? ? ? ? ? System.out.println("我是空!");// ? ? ? }// 優(yōu)化:// a. 先判斷是否是null,不為null時在判空if(list != null && list.isEmpty()){System.out.println("我是空!");}// 集合不為null時,也可以通過.size()方法判斷集合是否為空if(list != null && list.size() > 0){System.out.println("我是空!");}// b. 使用工具類判斷集合是否為空if(CollUtil.isEmpty(list)){System.out.println("我是空!");}?// 2. 向集合中添加元素產(chǎn)生空指針異常String key = null;String value = null;Hashtable<String,String> hashtable = new Hashtable<>();// ? ? ? hashtable.put(key,value);// 優(yōu)化:if(key != null && value != null){hashtable.put(key, value);}/*** 總結:* ? ? 分析:部分集合中不允許設置key或value為null,這類集合主要有:Hashtable、ConcurrentHashMap、ConcurrentSkipListSet、* ConcurrentLinkedDeque、ConcurrentLinkedQueue、LinkedBlockingDeque、LinkedBlockingQueue、ArrayBlockingQueue、* PriorityBlockingQueue等。*/}}
如有錯誤,歡迎批評指正!