首頁 > Java > java教程 > 主體

Java 空指標異常

王林
發布: 2024-08-30 16:13:03
原創
831 人瀏覽過

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

相關標籤:
來源:php
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!