NullReferenceException NullReferenceException NullReferenceException NullReferenceException Class

定義

當嘗試對 Null 物件取值時,所擲回的例外狀況。The exception that is thrown when there is an attempt to dereference a null object reference.

public ref class NullReferenceException : SystemException
[System.Runtime.InteropServices.ComVisible(true)]
[System.Serializable]
public class NullReferenceException : SystemException
type NullReferenceException = class
    inherit SystemException
Public Class NullReferenceException
Inherits SystemException
繼承
NullReferenceExceptionNullReferenceExceptionNullReferenceExceptionNullReferenceException
屬性

備註

ANullReferenceException當您嘗試存取的成員,其值的型別上時,會擲回例外狀況nullA NullReferenceException exception is thrown when you try to access a member on a type whose value is null. ANullReferenceException例外狀況通常會反映開發人員錯誤,並在下列情況擲回:A NullReferenceException exception typically reflects developer error and is thrown in the following scenarios:

  • 您忘記具現化參考型別。You've forgotten to instantiate a reference type. 在下列範例中,names已宣告但永遠不會具現化:In the following example, names is declared but never instantiated:

    using System;
    using System.Collections.Generic;
    
    public class Example
    {
       public static void Main(string[] args)
       {
          int value = Int32.Parse(args[0]);
          List<String> names;
          if (value > 0)
             names = new List<String>();
          
          names.Add("Major Major Major");       
       }
    }
    // Compilation displays a warning like the following:
    //    Example1.cs(10) : warning BC42104: Variable //names// is used before it 
    //    has been assigned a value. A null reference exception could result 
    //    at runtime.
    //    
    //          names.Add("Major Major Major")
    //          ~~~~~
    // The example displays output like the following output:
    //    Unhandled Exception: System.NullReferenceException: Object reference 
    //    not set to an instance of an object.
    //       at Example.Main()
    
    Imports System.Collections.Generic
    
    Module Example
       Public Sub Main()
          Dim names As List(Of String)
          names.Add("Major Major Major")       
       End Sub
    End Module
    ' Compilation displays a warning like the following:
    '    Example1.vb(10) : warning BC42104: Variable 'names' is used before it 
    '    has been assigned a value. A null reference exception could result 
    '    at runtime.
    '    
    '          names.Add("Major Major Major")
    '          ~~~~~
    ' The example displays output like the following output:
    '    Unhandled Exception: System.NullReferenceException: Object reference 
    '    not set to an instance of an object.
    '       at Example.Main()
    

    它們會編譯此程式碼時,某些編譯器會發出警告。Some compilers issue a warning when they compile this code. 其他人發出錯誤訊息,並編譯會失敗。Others issue an error, and the compilation fails. 若要解決此問題,具現化物件,使其值不會再nullTo address this problem, instantiate the object so that its value is no longer null. 下列範例會藉由呼叫類型的類別建構函式。The following example does this by calling a type's class constructor.

    using System;
    using System.Collections.Generic;
    
    public class Example
    {
       public static void Main()
       {
          List<String> names = new List<String>();
          names.Add("Major Major Major");
       }
    }
    
    Imports System.Collections.Generic
    
    Module Example
       Public Sub Main()
          Dim names As New List(Of String)()
          names.Add("Major Major Major")       
       End Sub
    End Module
    
  • 您忘記之前將它初始化維度陣列。You've forgotten to dimension an array before initializing it. 在下列範例中,values會宣告為整數陣列,但永遠不會指定它所包含的項目數。In the following example, values is declared to be an integer array, but the number of elements that it contains is never specified. 嘗試初始化其值,因此會擲回NullReferenceException例外狀況。The attempt to initialize its values therefore thrown a NullReferenceException exception.

    using System;
    
    public class Example
    {
       public static void Main()
       {
           int[] values = null;
           for (int ctr = 0; ctr <= 9; ctr++)
              values[ctr] = ctr * 2;
              
           foreach (var value in values)
              Console.WriteLine(value);   
       }
    }
    // The example displays the following output:
    //    Unhandled Exception: 
    //       System.NullReferenceException: Object reference not set to an instance of an object.
    //       at Example.Main()
    
    Module Example
       Public Sub Main()
           Dim values() As Integer
           For ctr As Integer = 0 To 9
              values(ctr) = ctr * 2
           Next
              
           For Each value In values
              Console.WriteLine(value)
           Next      
       End Sub
    End Module
    ' The example displays the following output:
    '    Unhandled Exception: 
    '       System.NullReferenceException: Object reference not set to an instance of an object.
    '       at Example.Main()
    

    您可以避免例外狀況之前初始化,宣告陣列中的項目數,如下列範例所示。You can eliminate the exception by declaring the number of elements in the array before initializing it, as the following example does.

    using System;
    
    public class Example
    {
       public static void Main()
       {
           int[] values = new int[10];
           for (int ctr = 0; ctr <= 9; ctr++)
              values[ctr] = ctr * 2;
              
           foreach (var value in values)
              Console.WriteLine(value);   
       }
    }
    // The example displays the following output:
    //    0
    //    2
    //    4
    //    6
    //    8
    //    10
    //    12
    //    14
    //    16
    //    18
    
    Module Example
       Public Sub Main()
           Dim values(9) As Integer
           For ctr As Integer = 0 To 9
              values(ctr) = ctr * 2
           Next
              
           For Each value In values
              Console.WriteLine(value)
           Next      
       End Sub
    End Module
    ' The example displays the following output:
    '    0
    '    2
    '    4
    '    6
    '    8
    '    10
    '    12
    '    14
    '    16
    '    18
    

    如需有關宣告和初始化陣列的詳細資訊,請參閱 < 陣列陣列For more information on declaring and initializing arrays, see Arrays and Arrays.

  • 得到null從方法傳回值,並接著傳回的型別上呼叫方法。You get a null return value from a method, and then call a method on the returned type. 這有時是文件錯誤; 的結果文件失敗時要注意的方法呼叫可以傳回nullThis sometimes is the result of a documentation error; the documentation fails to note that a method call can return null. 在其他情況下,您的程式碼錯誤地假設該方法一律會傳回非null值。In other cases, your code erroneously assumes that the method will always return a non-null value.

    在下列範例程式碼會假設Array.Find方法一律會傳回Person物件,其FirstName欄位符合搜尋字串。The code in the following example assumes that the Array.Find method always returns Person object whose FirstName field matches a search string. 因為沒有相符項目,所以執行階段會擲回NullReferenceException例外狀況。Because there is no match, the runtime throws a NullReferenceException exception.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          Person[] persons = Person.AddRange( new String[] { "Abigail", "Abra", 
                                              "Abraham", "Adrian", "Ariella", 
                                              "Arnold", "Aston", "Astor" } );    
          String nameToFind = "Robert";
          Person found = Array.Find(persons, p => p.FirstName == nameToFind);
          Console.WriteLine(found.FirstName);
       }
    }
    
    public class Person
    {
       public static Person[] AddRange(String[] firstNames) 
       {
          Person[] p = new Person[firstNames.Length];
          for (int ctr = 0; ctr < firstNames.Length; ctr++)
             p[ctr] = new Person(firstNames[ctr]);
    
          return p;
       }
       
       public Person(String firstName)
       {
          this.FirstName = firstName;
       } 
       
       public String FirstName;
    }
    // The example displays the following output:
    //       Unhandled Exception: System.NullReferenceException: 
    //       Object reference not set to an instance of an object.
    //          at Example.Main()
    
    Module Example
       Public Sub Main()
          Dim persons() As Person = Person.AddRange( { "Abigail", "Abra",
                                                       "Abraham", "Adrian",
                                                       "Ariella", "Arnold", 
                                                       "Aston", "Astor" } )    
          Dim nameToFind As String = "Robert"
          Dim found As Person = Array.Find(persons, Function(p) p.FirstName = nameToFind)
          Console.WriteLine(found.FirstName)
       End Sub
    End Module
    
    Public Class Person
       Public Shared Function AddRange(firstNames() As String) As Person()
          Dim p(firstNames.Length - 1) As Person
          For ctr As Integer = 0 To firstNames.Length - 1
             p(ctr) = New Person(firstNames(ctr))
          Next   
          Return p
       End Function
       
       Public Sub New(firstName As String)
          Me.FirstName = firstName
       End Sub 
       
       Public FirstName As String
    End Class
    ' The example displays the following output:
    '       Unhandled Exception: System.NullReferenceException: 
    '       Object reference not set to an instance of an object.
    '          at Example.Main()
    

    若要解決此問題,測試方法的傳回值,以確保它不是null之前呼叫它的任何成員,如下列範例所示。To address this problem, test the method's return value to ensure that it is not null before calling any of its members, as the following example does.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          Person[] persons = Person.AddRange( new String[] { "Abigail", "Abra", 
                                              "Abraham", "Adrian", "Ariella", 
                                              "Arnold", "Aston", "Astor" } );    
          String nameToFind = "Robert";
          Person found = Array.Find(persons, p => p.FirstName == nameToFind);
          if (found != null)
             Console.WriteLine(found.FirstName);
          else
             Console.WriteLine("{0} not found.", nameToFind);   
       }
    }
    
    public class Person
    {
       public static Person[] AddRange(String[] firstNames) 
       {
          Person[] p = new Person[firstNames.Length];
          for (int ctr = 0; ctr < firstNames.Length; ctr++)
             p[ctr] = new Person(firstNames[ctr]);
    
          return p;
       }
       
       public Person(String firstName)
       {
          this.FirstName = firstName;
       } 
       
       public String FirstName;
    }
    // The example displays the following output:
    //        Robert not found
    
    Module Example
       Public Sub Main()
          Dim persons() As Person = Person.AddRange( { "Abigail", "Abra",
                                                       "Abraham", "Adrian",
                                                       "Ariella", "Arnold", 
                                                       "Aston", "Astor" } )    
          Dim nameToFind As String = "Robert"
          Dim found As Person = Array.Find(persons, Function(p) p.FirstName = nameToFind)
          If found IsNot Nothing Then
             Console.WriteLine(found.FirstName)
          Else
             Console.WriteLine("{0} not found.", nameToFind)
          End If   
       End Sub
    End Module
    
    Public Class Person
       Public Shared Function AddRange(firstNames() As String) As Person()
          Dim p(firstNames.Length - 1) As Person
          For ctr As Integer = 0 To firstNames.Length - 1
             p(ctr) = New Person(firstNames(ctr))
          Next   
          Return p
       End Function
       
       Public Sub New(firstName As String)
          Me.FirstName = firstName
       End Sub 
       
       Public FirstName As String
    End Class
    ' The example displays the following output:
    '       Robert not found
    
  • 您使用運算式 (例如,您要鏈結方法或屬性的清單) 來擷取值,雖然您檢查值是否null,執行階段仍會擲回NullReferenceException例外狀況。You're using an expression (for example, you're chaining a list of methods or properties together) to retrieve a value and, although you're checking whether the value is null, the runtime still throws a NullReferenceException exception. 這是因為其中一個運算式中的中間值傳回nullThis occurs because one of the intermediate values in the expression returns null. 如此一來,您的 restnull永遠不會進行評估。As a result, your rest for null is never evaluated.

    下列範例會定義Pages快取網頁,會帶來的相關資訊的物件Page物件。The following example defines a Pages object that caches information about web pages, which are presented by Page objects. Example.Main方法會檢查是否目前 web 網頁有非 null 的標題,若是如此,所顯示的標題。The Example.Main method checks whether the current web page has a non-null title and, if it does, displays the title. 儘管這項檢查,不過,方法會擲回NullReferenceException例外狀況。Despite this check, however, the method throws a NullReferenceException exception.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          var pages = new Pages();
          if (! String.IsNullOrEmpty(pages.CurrentPage.Title)) {
             String title = pages.CurrentPage.Title;
             Console.WriteLine("Current title: '{0}'", title);
          }
       }
    }
    
    public class Pages 
    {
       Page[] page = new Page[10];
       int ctr = 0;
       
       public Page CurrentPage
       {
          get { return page[ctr]; }
          set {
             // Move all the page objects down to accommodate the new one.
             if (ctr > page.GetUpperBound(0)) {
                for (int ndx = 1; ndx <= page.GetUpperBound(0); ndx++)
                   page[ndx - 1] = page[ndx];
             }    
             page[ctr] = value;
             if (ctr < page.GetUpperBound(0))
                ctr++; 
          }
       }
       
       public Page PreviousPage
       {
          get {
             if (ctr == 0) { 
                if (page[0] == null)
                   return null;
                else
                   return page[0];
             }
             else {
                ctr--;
                return page[ctr + 1];
             }
          }
       }         
    }
    
    public class Page
    {
       public Uri URL;
       public String Title;
    }
    // The example displays the following output:
    //    Unhandled Exception: 
    //       System.NullReferenceException: Object reference not set to an instance of an object.
    //       at Example.Main()
    
    Module Example
       Public Sub Main()
          Dim pages As New Pages()
          Dim title As String = pages.CurrentPage.Title
       End Sub
    End Module
    
    Public Class Pages 
       Dim page(9) As Page
       Dim ctr As Integer = 0
       
       Public Property CurrentPage As Page
          Get
             Return page(ctr)
          End Get
          Set
             ' Move all the page objects down to accommodate the new one.
             If ctr > page.GetUpperBound(0) Then
                For ndx As Integer = 1 To page.GetUpperBound(0)
                   page(ndx - 1) = page(ndx)
                Next
             End If    
             page(ctr) = value
             If ctr < page.GetUpperBound(0) Then ctr += 1 
          End Set
       End Property
       
       Public ReadOnly Property PreviousPage As Page
          Get
             If ctr = 0 Then 
                If page(0) Is Nothing Then
                   Return Nothing
                Else
                   Return page(0)
                End If   
             Else
                ctr -= 1
                Return page(ctr + 1)
             End If
          End Get
       End Property         
    End Class
    
    Public Class Page
       Public URL As Uri
       Public Title As String
    End Class
    ' The example displays the following output:
    '    Unhandled Exception: 
    '       System.NullReferenceException: Object reference not set to an instance of an object.
    '       at Example.Main()
    

    會擲回例外狀況,因為pages.CurrentPage傳回null如果沒有頁面資訊儲存在快取。The exception is thrown because pages.CurrentPage returns null if no page information is stored in the cache. 這個例外狀況可由測試值的更正CurrentPage屬性,才能擷取目前Page物件的Title屬性,如下列範例會:This exception can be corrected by testing the value of the CurrentPage property before retrieving the current Page object's Title property, as the following example does:

    using System;
    
    public class Example
    {
       public static void Main()
       {
          var pages = new Pages();
          Page current = pages.CurrentPage;
          if (current != null) {  
             String title = current.Title;
             Console.WriteLine("Current title: '{0}'", title);
          }
          else {
             Console.WriteLine("There is no page information in the cache.");
          }   
       }
    }
    // The example displays the following output:
    //       There is no page information in the cache.
    
    Module Example
       Public Sub Main()
          Dim pages As New Pages()
          Dim current As Page = pages.CurrentPage
          If current IsNot Nothing Then 
             Dim title As String = current.Title
             Console.WriteLine("Current title: '{0}'", title)
          Else
             Console.WriteLine("There is no page information in the cache.")
          End If   
       End Sub
    End Module
    ' The example displays the following output:
    '       There is no page information in the cache.
    
  • 您要列舉的陣列,其中包含參考型別,與您嘗試將其中一個項目就會擲回的程序項目NullReferenceException例外狀況。You're enumerating the elements of an array that contains reference types, and your attempt to process one of the elements throws a NullReferenceException exception.

    下列範例會定義一個字串陣列。The following example defines a string array. Afor陳述式列舉陣列中的項目,並呼叫每個字串Trim方法,然後再顯示字串。A for statement enumerates the elements in the array and calls each string's Trim method before displaying the string.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          String[] values = { "one", null, "two" };
          for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
             Console.Write("{0}{1}", values[ctr].Trim(), 
                           ctr == values.GetUpperBound(0) ? "" : ", "); 
          Console.WriteLine();
       }
    }
    // The example displays the following output:
    //    Unhandled Exception: 
    //       System.NullReferenceException: Object reference not set to an instance of an object.
    //       at Example.Main()
    
    Module Example
       Public Sub Main()
          Dim values() As String = { "one", Nothing, "two" }
          For ctr As Integer = 0 To values.GetUpperBound(0)
             Console.Write("{0}{1}", values(ctr).Trim(), 
                           If(ctr = values.GetUpperBound(0), "", ", ")) 
          Next
          Console.WriteLine()
       End Sub
    End Module
    ' The example displays the following output:
    '    Unhandled Exception: System.NullReferenceException: 
    '       Object reference not set to an instance of an object.
    '       at Example.Main()
    

    如果您假設陣列的每個項目必須包含非 null 值,而且事實上是陣列元素的值,就會發生這個例外狀況nullThis exception occurs if you assume that each element of the array must contain a non-null value, and the value of the array element is in fact null. 例外狀況會消除,可以藉由測試項目是否為null之前執行任何作業,該元素,如下列範例所示。The exception can be eliminated by testing whether the element is null before performing any operation on that element, as the following example shows.

    using System;
    
    public class Example
    {
       public static void Main()
       {
          String[] values = { "one", null, "two" };
          for (int ctr = 0; ctr <= values.GetUpperBound(0); ctr++)
             Console.Write("{0}{1}", 
                           values[ctr] != null ? values[ctr].Trim() : "", 
                           ctr == values.GetUpperBound(0) ? "" : ", "); 
          Console.WriteLine();
       }
    }
    // The example displays the following output:
    //       one, , two
    
    Module Example
       Public Sub Main()
          Dim values() As String = { "one", Nothing, "two" }
          For ctr As Integer = 0 To values.GetUpperBound(0)
             Console.Write("{0}{1}", 
                           If(values(ctr) IsNot Nothing, values(ctr).Trim(), ""), 
                           If(ctr = values.GetUpperBound(0), "", ", ")) 
          Next
          Console.WriteLine()
       End Sub
    End Module
    ' The example displays the following output:
    '       one, , two
    
  • ANullReferenceException傳遞方法所擲回例外狀況nullA NullReferenceException exception is thrown by a method that is passed null. 某些方法會驗證傳遞給它們的引數。Some methods validate the arguments that are passed to them. 如果這樣做,而且其中一個引數null,方法會擲回System.ArgumentNullException例外狀況。If they do and one of the arguments is null, the method throws an System.ArgumentNullException exception. 否則,會擲回NullReferenceException例外狀況。Otherwise, it throws a NullReferenceException exception. 下列範例說明此案例。The following example illustrates this scenario.

    using System;
    using System.Collections.Generic;
    
    public class Example
    {
       public static void Main()
       {
          List<String> names = GetData();
          PopulateNames(names);
       }
    
       private static void PopulateNames(List<String> names)
       {
          String[] arrNames = { "Dakota", "Samuel", "Nikita",
                                "Koani", "Saya", "Yiska", "Yumaevsky" };
          foreach (var arrName in arrNames)
             names.Add(arrName);
       }
       
       private static List<String> GetData() 
       {
          return null;   
       
       }
    }
    // The example displays output like the following:
    //    Unhandled Exception: System.NullReferenceException: Object reference 
    //    not set to an instance of an object.
    //       at Example.PopulateNames(List`1 names)
    //       at Example.Main()
    
    Imports System.Collections.Generic
    
    Module Example
       Public Sub Main()
          Dim names As List(Of String) = GetData()
          PopulateNames(names)
       End Sub
       
       Private Sub PopulateNames(names As List(Of String))
          Dim arrNames() As String = { "Dakota", "Samuel", "Nikita",
                                       "Koani", "Saya", "Yiska", "Yumaevsky" }
          For Each arrName In arrNames
             names.Add(arrName)
          Next
       End Sub
       
       Private Function GetData() As List(Of String)
          Return Nothing   
       End Function
    End Module
    ' The example displays output like the following:
    '    Unhandled Exception: System.NullReferenceException: Object reference 
    '    not set to an instance of an object.
    '       at Example.PopulateNames(List`1 names)
    '       at Example.Main()
    

    若要解決此問題,請確定傳遞給方法的引數不是null,或處理中擲回的例外狀況try…catch…finally區塊。To address this issue, make sure that the argument passed to the method is not null, or handle the thrown exception in a try…catch…finally block. 如需詳細資訊,請參閱 例外狀況中定義的介面的私用 C++ 專屬實作。For more information, see Exceptions.

下列的 Microsoft intermediate language (MSIL) 指令會擲回NullReferenceException: callvirtcpblkcpobjinitblkldelem.<type>ldelemaldfldldfldaldind.<type>``ldlenstelem.<type>stfldstind.<type>throw,和unboxThe following Microsoft intermediate language (MSIL) instructions throw NullReferenceException: callvirt, cpblk, cpobj, initblk, ldelem.<type>, ldelema, ldfld, ldflda, ldind.<type>, ldlen, stelem.<type>, stfld, stind.<type>, throw, and unbox.

NullReferenceException 會使用 HRESULT COR_E_NULLREFERENCE,其值 0x80004003。NullReferenceException uses the HRESULT COR_E_NULLREFERENCE, which has the value 0x80004003.

如需執行個體的初始屬性值的清單NullReferenceException,請參閱NullReferenceException建構函式。For a list of initial property values for an instance of NullReferenceException, see the NullReferenceException constructors.

在 發行程式碼中處理 NullReferenceExceptionHandling NullReferenceException in release code

它通常會比較好以避免 NullReferenceException,比處理之後,就會發生。It's usually better to avoid a NullReferenceException than to handle it after it occurs. 處理例外狀況會讓您的程式碼更難維護及了解,而且有時候會引進其他 Bug。Handling an exception can make your code harder to maintain and understand, and can sometimes introduce other bugs. NullReferenceException 通常是無法修復的錯誤。A NullReferenceException is often a non-recoverable error. 在這些案例中,讓例外狀況停止應用程式可能是最好的替代方案。In these cases, letting the exception stop the app might be the best alternative.

不過,有許多情況中處理錯誤會有幫助。However, there are many situations where handling the error can be useful:

  • 您的應用程式可以忽略 null 的物件。Your app can ignore objects that are null. 例如,如果您的應用程式會擷取並處理資料庫中的記錄,也許您可以忽略某些會產生 null 物件的錯誤記錄。For example, if your app retrieves and processes records in a database, you might be able to ignore some number of bad records that result in null objects. 將錯誤資料記錄在記錄檔或應用程式 UI 中可能是您唯一必須做的事。Recording the bad data in a log file or in the application UI might be all you have to do.

  • 您可以從例外狀況復原。You can recover from the exception. 例如,連接中斷或逾時,如果呼叫會傳回參考類型的 Web 服務可能會傳回 null。您可以嘗試重新建立連接,並試著再呼叫一次。For example, a call to a web service that returns a reference type might return null if the connection is lost or the connection times out. You can attempt to reestablish the connection and try the call again.

  • 您可以將應用程式的狀態還原至有效狀態。You can restore the state of your app to a valid state. 例如,您執行的多步驟工作可能會需要您先將資訊儲存至資料儲存區,才能呼叫擲回 NullReferenceException 的方法。For example, you might be performing a multi-step task that requires you to save information to a data store before you call a method that throws a NullReferenceException. 如果未初始化的物件會損毀資料記錄,您可以先移除之前的資料再關閉應用程式。If the uninitialized object would corrupt the data record, you can remove the previous data before you close the app.

  • 您想要回報該例外狀況。You want to report the exception. 例如,如果錯誤由應用程式使用者的錯誤所造成,您可以產生訊息來幫助他提供正確的資訊。For example, if the error was caused by a mistake from the user of your app, you can generate a message to help him supply the correct information. 您也可以記錄錯誤的資訊,以幫助您修正問題。You can also log information about the error to help you fix the problem. 有些架構,像 ASP.NET,具有高層級且可以擷取所有錯誤的例外狀況處理常式,此時應用程式永遠不會損毀,在這種情況下,記錄例外狀況可能是您可以知道它有發生的唯一方法。Some frameworks, like ASP.NET, have a high-level exception handler that captures all errors to that the app never crashes; in that case, logging the exception might be the only way you can know that it occurs.

建構函式

NullReferenceException() NullReferenceException() NullReferenceException() NullReferenceException()

初始化 NullReferenceException 類別的新執行個體,設定該新執行個體的 Message 屬性為描述錯誤的系統提供訊息,例如「當需要物件的執行個體時,找不到 'Null' 值」。Initializes a new instance of the NullReferenceException class, setting the Message property of the new instance to a system-supplied message that describes the error, such as "The value 'null' was found where an instance of an object was required." 此訊息會考量目前的系統文化特性。This message takes into account the current system culture.

NullReferenceException(SerializationInfo, StreamingContext) NullReferenceException(SerializationInfo, StreamingContext) NullReferenceException(SerializationInfo, StreamingContext) NullReferenceException(SerializationInfo, StreamingContext)

使用序列化資料,初始化 NullReferenceException 類別的新執行個體。Initializes a new instance of the NullReferenceException class with serialized data.

NullReferenceException(String) NullReferenceException(String) NullReferenceException(String) NullReferenceException(String)

使用指定的錯誤訊息,初始化 NullReferenceException 類別的新執行個體。Initializes a new instance of the NullReferenceException class with a specified error message.

NullReferenceException(String, Exception) NullReferenceException(String, Exception) NullReferenceException(String, Exception) NullReferenceException(String, Exception)

使用指定的錯誤訊息以及造成此例外狀況的內部例外狀況的參考,初始化 NullReferenceException 類別的新執行個體。Initializes a new instance of the NullReferenceException class with a specified error message and a reference to the inner exception that is the cause of this exception.

屬性

Data Data Data Data

取得提供例外狀況之其他使用者定義相關資訊的索引鍵/值組集合。Gets a collection of key/value pairs that provide additional user-defined information about the exception.

(Inherited from Exception)
HelpLink HelpLink HelpLink HelpLink

取得或設定與這個例外狀況相關聯的說明檔連結。Gets or sets a link to the help file associated with this exception.

(Inherited from Exception)
HResult HResult HResult HResult

取得或設定 HRESULT,它是指派給特定例外狀況的編碼數值。Gets or sets HRESULT, a coded numerical value that is assigned to a specific exception.

(Inherited from Exception)
InnerException InnerException InnerException InnerException

取得造成目前例外狀況的 Exception 執行個體。Gets the Exception instance that caused the current exception.

(Inherited from Exception)
Message Message Message Message

取得描述目前例外狀況的訊息。Gets a message that describes the current exception.

(Inherited from Exception)
Source Source Source Source

取得或設定造成錯誤的應用程式或物件的名稱。Gets or sets the name of the application or the object that causes the error.

(Inherited from Exception)
StackTrace StackTrace StackTrace StackTrace

取得呼叫堆疊上即時運算框架的字串表示。Gets a string representation of the immediate frames on the call stack.

(Inherited from Exception)
TargetSite TargetSite TargetSite TargetSite

取得擲回目前例外狀況的方法。Gets the method that throws the current exception.

(Inherited from Exception)

方法

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetBaseException() GetBaseException() GetBaseException() GetBaseException()

在衍生類別中覆寫時,傳回一或多個後續的例外狀況的根本原因 ExceptionWhen overridden in a derived class, returns the Exception that is the root cause of one or more subsequent exceptions.

(Inherited from Exception)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

在衍生類別中覆寫時,使用例外狀況的資訊設定 SerializationInfoWhen overridden in a derived class, sets the SerializationInfo with information about the exception.

(Inherited from Exception)
GetType() GetType() GetType() GetType()

取得目前執行個體的執行階段類型。Gets the runtime type of the current instance.

(Inherited from Exception)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複製。Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

建立並傳回目前例外狀況的字串表示。Creates and returns a string representation of the current exception.

(Inherited from Exception)

事件

SerializeObjectState SerializeObjectState SerializeObjectState SerializeObjectState

當例外狀況序列化,以建立包含例外狀況相關序列化資料的例外狀況狀態物件時,就會發生此事件。Occurs when an exception is serialized to create an exception state object that contains serialized data about the exception.

(Inherited from Exception)

適用於

另請參閱