目錄
NullPointerException 在 Java 中如何運作?
Java NullPointerException 的建構子
Java NullPointerException 的實作範例
範例#2
Example #3
How to Avoid NullPointerException?
Conclusion
首頁 Java java教程 Java 空指標異常

Java 空指標異常

Aug 30, 2024 pm 04:13 PM
java

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.");
}
}
}
登入後複製

輸出:

Java 空指標異常

範例#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.");
}
}
}
登入後複製

輸出:

Java 空指標異常

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:

Java 空指標異常

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中文網其他相關文章!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

<🎜>:泡泡膠模擬器無窮大 - 如何獲取和使用皇家鑰匙
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系統,解釋
1 個月前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆樹的耳語 - 如何解鎖抓鉤
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
<🎜>掩蓋:探險33-如何獲得完美的色度催化劑
2 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

熱門話題

Java教學
1677
14
CakePHP 教程
1430
52
Laravel 教程
1333
25
PHP教程
1278
29
C# 教程
1257
24
PHP與Python:了解差異 PHP與Python:了解差異 Apr 11, 2025 am 12:15 AM

PHP和Python各有優勢,選擇應基於項目需求。 1.PHP適合web開發,語法簡單,執行效率高。 2.Python適用於數據科學和機器學習,語法簡潔,庫豐富。

PHP:網絡開發的關鍵語言 PHP:網絡開發的關鍵語言 Apr 13, 2025 am 12:08 AM

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

PHP與其他語言:比較 PHP與其他語言:比較 Apr 13, 2025 am 12:19 AM

PHP適合web開發,特別是在快速開發和處理動態內容方面表現出色,但不擅長數據科學和企業級應用。與Python相比,PHP在web開發中更具優勢,但在數據科學領域不如Python;與Java相比,PHP在企業級應用中表現較差,但在web開發中更靈活;與JavaScript相比,PHP在後端開發中更簡潔,但在前端開發中不如JavaScript。

PHP與Python:核心功能 PHP與Python:核心功能 Apr 13, 2025 am 12:16 AM

PHP和Python各有優勢,適合不同場景。 1.PHP適用於web開發,提供內置web服務器和豐富函數庫。 2.Python適合數據科學和機器學習,語法簡潔且有強大標準庫。選擇時應根據項目需求決定。

PHP的影響:網絡開發及以後 PHP的影響:網絡開發及以後 Apr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP:許多網站的基礎 PHP:許多網站的基礎 Apr 13, 2025 am 12:07 AM

PHP成為許多網站首選技術棧的原因包括其易用性、強大社區支持和廣泛應用。 1)易於學習和使用,適合初學者。 2)擁有龐大的開發者社區,資源豐富。 3)廣泛應用於WordPress、Drupal等平台。 4)與Web服務器緊密集成,簡化開發部署。

PHP與Python:用例和應用程序 PHP與Python:用例和應用程序 Apr 17, 2025 am 12:23 AM

PHP適用於Web開發和內容管理系統,Python適合數據科學、機器學習和自動化腳本。 1.PHP在構建快速、可擴展的網站和應用程序方面表現出色,常用於WordPress等CMS。 2.Python在數據科學和機器學習領域表現卓越,擁有豐富的庫如NumPy和TensorFlow。

H5:工具,框架和最佳實踐 H5:工具,框架和最佳實踐 Apr 11, 2025 am 12:11 AM

H5開發需要掌握的工具和框架包括Vue.js、React和Webpack。 1.Vue.js適用於構建用戶界面,支持組件化開發。 2.React通過虛擬DOM優化頁面渲染,適合複雜應用。 3.Webpack用於模塊打包,優化資源加載。

See all articles