C# NullReferenceException

WBOY
發布: 2024-09-03 15:21:04
原創
897 人瀏覽過

NullReferenceException 是當我們嘗試存取任何值為 null 的成員類型時程式拋出的異常,這表示當我們嘗試存取沒有值或為 null 值的變數時,將出現 Null Reference 異常值被丟棄。此例外適用於 .NET、.NET Core 和 .Net Framework 的各個版本。 C#中的這些引用變數與C中的指標概念非常吻合。發生NullReferenceException的情況有很多種,而且有多種方法可以避免或解決它。

文法:

以下是用來實作 NullReferenceException 的標準語法:

public class NullReferenceException :SystemException
登入後複製

空引用異常繼承自系統異常,系統異常基本上可以在物件內部和異常中找到。眾所周知,這是最常見的異常之一,有多種方法可以處理它。

NullReferenceException 在 C# 中如何運作?

簡單來說,空引用異常是我們嘗試存取未引用任何其他物件的變數的事件的結果。現在,引用引用在這裡不是問題,但是當引用變數沒有引用任何其他物件時,它基本上被視為 null。當程式碼參考的點最終變成 null 時,就會出現問題,然後我們會遇到名為 NullReferenceException 的例外。程式可能會在多種情況下引發空引用異常。當我們執行一個程式時,如果遇到空引用異常,輸出會是這樣的:

未處理的異常:
System.NullReferenceException:未將物件參考設定到物件的實例。

範例

現在我們已經了解了異常是什麼以及它是如何運作的,讓我們開始用範例正確地示範異常。對於我們的第一個範例,非常簡單,我們有一個保存空值的簡單變量,然後我們將嘗試處理該變量,但由於是空值,它將拋出空引用異常。程式碼如下:

代碼:

using System;
public class SampleProgram {
public static void Main()     {
string name = null;
varval =name.ToString();
Console.WriteLine(val);
}
}
登入後複製

程式碼說明:使用System聲明,我們有我們的類別Sample,它是公開的。然後我們有 static void main 語句,然後建立一個名為 name 的簡單字串變量,分配的值為 null,這表示該變數沒有值。這個字串變數在這裡很重要,稍後我們會建立另一個名為 val 的變量,我們嘗試將 name 的值轉換為字串。最後,我們有一個 print 語句,它將列印 name 的值,現在使用 ToString() 轉換該值。請參閱下面所附的輸出螢幕截圖:

輸出:

C# NullReferenceException

如果正確執行,程式碼會拋出一個錯誤,即NullReferenceException。原因是當我們嘗試呼叫 ToString() 方法時,它將轉到變數名,但我們的變數名稱沒有值,即 null。眾所周知,空值不能使用 ToString() 來轉換。所以我們的程式碼只會列印一個錯誤,這意味著程式碼正在如預期運行。

如所解釋的,程式已因異常而終止。接下來,我們將演示另一個簡單的範例,正如所解釋的那樣,它會導致相同的異常。

代碼:

using System;
class SampleProgram {
static void Main() {
string val = null;
if (val.Length == 0) {
Console.WriteLine(val);
}
}
}
登入後複製

程式碼說明:與我們的第一個範例類似,這裡我們有命名空間和第一個調用,其中包含主語句。然後我們的字串變數的值為 null。這將是主要變量,它將導致我們預期的異常。然後我們有一個簡單的 if 條件,我們將檢查變數的長度是否為零,如果為零,它將進入下一步並列印該值。但程式碼不會移動到最後的列印行,因為它會在 if 內遇到異常。請參閱下面所附的輸出螢幕截圖:

輸出:

C# NullReferenceException

這裡,輸出就像我們的第一個範例“未處理的異常”,因為異常是相同的,我們嘗試在這裡實現一個函數,但正如所解釋的,我們的變數有一個空值,這導致我們出現空引用異常。現在我們已經看到並理解了這個空引用異常是如何以及為什麼發生的,了解如何避免它以使程式更好地運行非常重要。

How to Avoid NullReferenceException in C#?

The Null Reference Exception is not a major error, but one of the common ones and one of the basic and simple way to avoid the Null Reference Exception is to check the variable or property before moving ahead and accessing it. And a very basic way to do this is to check the variable within an if statement. We will demonstrate an example where we will avoid the occurrence of the exception and the code will move on.

Code:

using System;
class SampleProgram {
static void Main() {
string val = null;
if (val == null) {
Console.WriteLine("\n Value to the variable is null.");
}
else{
Console.WriteLine(val);
}
}
}
登入後複製

Output:

C# NullReferenceException

Code Explanation: Here we have our class which holds the main statement than a variable with a null value. Then we enter an if else statement, where the value of the variable is checked if it is null, the print statement will be printed and the program will terminate, if the value is not null, then it will move ahead and into else part, it will print the value. As expected our code printed that “Value to the variable is null.” because the value is null. If we try the same example with a string value, the program will proceed and the else part will be printed.

Conclusion

The NullReferenceException is encountered when we attempt to access a variable which holds a null value, it can be variable or object. The reference should not hold null value else the exception will be thrown. There are many situations where this can be seen and the simplest way to avoid the NullReferenceException is to check beforehand, before accessing the value.

以上是C# NullReferenceException的詳細內容。更多資訊請關注PHP中文網其他相關文章!

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