NullPointer Exception是java中程式設計師常出現的異常。這是當物件實例化不正確時發生的運行時異常。該物件被聲明為其中包含空值。空指標異常僅表示它正在嘗試使用其中包含空值的參考來呼叫物件。最重要的是要記住,空指標異常與指標無關,因為 java 語言不支援指標概念;相反,它與物件引用相關聯。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
文法:
沒有特定的語法將空指標異常指定為運行時錯誤;它會自動出現並變得可見以供使用。
可以宣告並拋出一個catch異常來指向和調試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); }
NullPointerException 在 Java 中如何運作?
Java 中的 NullPointerException 是一個對開發人員來說根本不是一個更好的錯誤的例外。異常很難發現,因為它每次都會在運行時發生。因此,找到這樣的異常是一項繁瑣的任務。程式設計師需要花費幾個小時才能找出 NullPointerException。
當其他事情需要物件時,在任何地方不尋常地嘗試指派 null 定義的物件時,就會出現這種情況。所有java錯誤,包括NullPointerException,都是因為某些原因而發生的,例如:
- 每當聲明 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)
這個建構函式的行為與 NullPointerException() 相矛盾,因為它包含了在指定位置引起的一些詳細訊息,並且它將涉及在建構空指標異常時可以使用的參數。參數 String s 負責建立帶有詳細訊息的空指標異常。
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
此程序說明了 java 程序,它避免了 NullPointerException 的創建,因為它不是常規方法。
代碼:
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 空指標異常的詳細內容。更多資訊請關注PHP中文網其他相關文章!

本篇文章给大家带来了关于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 Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

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)