NullReferenceException 类
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
尝试取消引用空对象引用时引发的异常。
public ref class NullReferenceException : Exception
public ref class NullReferenceException : SystemException
public class NullReferenceException : Exception
public class NullReferenceException : SystemException
[System.Serializable]
public class NullReferenceException : SystemException
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public class NullReferenceException : SystemException
type NullReferenceException = class
inherit Exception
type NullReferenceException = class
inherit SystemException
[<System.Serializable>]
type NullReferenceException = class
inherit SystemException
[<System.Serializable>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type NullReferenceException = class
inherit SystemException
Public Class NullReferenceException
Inherits Exception
Public Class NullReferenceException
Inherits SystemException
- 继承
- 继承
- 属性
注解
NullReferenceException尝试访问其值为 null
的类型的成员时,将引发异常。 异常 NullReferenceException 通常反映开发人员错误,并在以下情况下引发:
你忘记了实例化引用类型。 在以下示例中,
names
已声明,但从未实例化: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()
open System [<EntryPoint>] let main args = let value = Int32.Parse args[0] // Set names to null, don't initialize it. let mutable names = Unchecked.defaultof<ResizeArray<string>> if value > 0 then names <- ResizeArray() names.Add "Major Major Major" 0 // Compilation does not display a warning as this is an extremely rare occurance in F#. // Creating a value without initalizing either requires using 'null' (not possible // on types defined in F# without [<AllowNullLiteral>]) or Unchecked.defaultof. // // 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()
某些编译器在编译此代码时发出警告。 另一些则发出错误,编译失败。 若要解决此问题,请实例化 对象,使其值不再是
null
。 以下示例通过调用类型的 类构造函数来执行此操作。using System; using System.Collections.Generic; public class Example { public static void Main() { List<String> names = new List<String>(); names.Add("Major Major Major"); } }
let names = ResizeArray() 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
你忘记了在初始化数组之前对其进行维度设置。 在以下示例中,
values
声明为整数数组,但从未指定它所包含的元素数。 因此,尝试初始化其值会 NullReferenceException 引发异常。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()
let values: int[] = null for i = 0 to 9 do values[i] <- i * 2 for value in values do printfn $"{value}" // The example displays the following output: // Unhandled Exception: // System.NullReferenceException: Object reference not set to an instance of an object. // at <StartupCode$fs>.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()
可以通过在初始化数组之前声明数组中的元素数来消除异常,如以下示例所示。
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
let values = Array.zeroCreate<int> 10 for i = 0 to 9 do values[i] <- i * 2 for value in values do printfn $"{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
从方法获取 null 返回值,然后对返回的类型调用方法。 这有时是文档错误的结果;文档未能注意到方法调用可以返回
null
。 在其他情况下,代码错误地假定该方法将始终返回非 null 值。以下示例中的代码假定 Array.Find 方法始终返回
Person
其FirstName
字段与搜索字符串匹配的对象。 由于没有匹配项,运行时会 NullReferenceException 引发异常。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()
open System type Person(firstName) = member _.FirstName = firstName static member AddRange(firstNames) = Array.map Person firstNames let persons = [| "Abigail"; "Abra"; "Abraham"; "Adrian" "Ariella"; "Arnold"; "Aston"; "Astor" |] |> Person.AddRange let nameToFind = "Robert" let found = Array.Find(persons, fun p -> p.FirstName = nameToFind) printfn $"{found.FirstName}" // The example displays the following output: // Unhandled Exception: System.NullReferenceException: // Object reference not set to an instance of an object. // at <StartupCode$fs>.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
调用其任何成员之前,如以下示例所示。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
open System [<AllowNullLiteral>] type Person(firstName) = member _.FirstName = firstName static member AddRange(firstNames) = Array.map Person firstNames let persons = [| "Abigail"; "Abra"; "Abraham"; "Adrian" "Ariella"; "Arnold"; "Aston"; "Astor" |] |> Person.AddRange let nameToFind = "Robert" let found = Array.Find(persons, fun p -> p.FirstName = nameToFind) if found <> null then printfn $"{found.FirstName}" else printfn $"{nameToFind} not found." // Using F#'s Array.tryFind function // This does not require a null check or [<AllowNullLiteral>] let found2 = persons |> Array.tryFind (fun p -> p.FirstName = nameToFind) match found2 with | Some firstName -> printfn $"{firstName}" | None -> printfn $"{nameToFind} not found." // The example displays the following output: // Robert not found. // 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 引发异常。 这是因为表达式中的一个中间值返回null
。 因此,永远不会评估对null
的测试。以下示例定义一个
Pages
对象,该对象缓存由 对象呈现的Page
网页相关信息。 方法Example.Main
检查当前网页是否具有非 null 标题,如果标题为非 null,则显示标题。 但是,尽管检查,该方法仍会NullReferenceException引发异常。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()
open System type Page() = [<DefaultValue>] val mutable public URL: Uri [<DefaultValue>] val mutable public Title: string type Pages() = let pages = Array.zeroCreate<Page> 10 let mutable i = 0 member _.CurrentPage with get () = pages[i] and set (value) = // Move all the page objects down to accommodate the new one. if i > pages.GetUpperBound 0 then for ndx = 1 to pages.GetUpperBound 0 do pages[ndx - 1] <- pages[ndx] pages[i] <- value if i < pages.GetUpperBound 0 then i <- i + 1 member _.PreviousPage = if i = 0 then if box pages[0] = null then Unchecked.defaultof<Page> else pages[0] else i <- i - 1 pages[i + 1] let pages = Pages() if String.IsNullOrEmpty pages.CurrentPage.Title |> not then let title = pages.CurrentPage.Title printfn $"Current title: '{title}'" // The example displays the following output: // Unhandled Exception: // System.NullReferenceException: Object reference not set to an instance of an object. // at <StartupCode$fs>.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
。 可以通过在检索当前Page
对象的Title
属性之前测试 属性的值CurrentPage
来更正此异常,如以下示例所示: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.
let pages = Pages() let current = pages.CurrentPage if box current <> null then let title = current.Title printfn $"Current title: '{title}'" else printfn "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 元素会引发异常。
以下示例定义一个字符串数组。
for
语句枚举数组中的元素,并在显示字符串之前调用每个字符串的 Trim 方法。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()
open System let values = [| "one"; null; "two" |] for i = 0 to values.GetUpperBound 0 do printfn $"""{values[i].Trim()}{if i = values.GetUpperBound 0 then "" else ", "}""" printfn "" // The example displays the following output: // Unhandled Exception: // System.NullReferenceException: Object reference not set to an instance of an object. // at <StartupCode$fs>.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 值,并且数组元素的值实际上是
null
,则会发生此异常。 可以通过在对该元素执行任何操作之前测试元素是否为null
来消除异常,如以下示例所示。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
open System let values = [| "one"; null; "two" |] for i = 0 to values.GetUpperBound 0 do printf $"""{if values[i] <> null then values[i].Trim() else ""}{if i = values.GetUpperBound 0 then "" else ", "}""" 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
方法 NullReferenceException 在访问其参数之一的成员时,可能会引发异常,但该参数为
null
。PopulateNames
以下示例中的 方法在行names.Add(arrName);
中引发异常。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()
let populateNames (names: ResizeArray<string>) = let arrNames = [ "Dakota"; "Samuel"; "Nikita" "Koani"; "Saya"; "Yiska"; "Yumaevsky" ] for arrName in arrNames do names.Add arrName let getData () : ResizeArray<string> = null let names = getData () populateNames names // 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 <StartupCode$fs>.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
引发的异常。 有关更多信息,请参见 异常中定义的接口的私有 C++ 特定实现。
以下 Microsoft 中间语言 (MSIL) 指令引发 NullReferenceException:、、cpblk
、cpobj
、ldfld
ldelem.<type>
initblk
ldelema
、ldflda
stind.<type>
ldind.<type>
stelem.<type>
ldlen
stfld
、、、、 throw
和 。unbox
callvirt
NullReferenceException 使用 HRESULT COR_E_NULLREFERENCE,其值0x80004003。
有关实例的初始属性值的列表NullReferenceException,请参阅NullReferenceException构造函数。
在发布代码中处理 NullReferenceException
通常,避免 NullReferenceException 比在发生 NullReferenceException 后处理它更好。 处理异常会导致代码更难维护和理解,并且有时会引入其他 bug。 NullReferenceException 通常为不可恢复的错误。 在这些情况下,让异常停止应用可能是最佳替代方法。
然而,在许多情况下,处理错误可能非常有用:
你的应用可以忽略为 null 的对象。 例如,如果你的应用检索并处理数据库中的记录,则你可以忽略一些导致 null 对象的错误记录。 你可能仅需在日志文件或应用程序 UI 中记录错误数据。
可以从异常中恢复。 例如,如果连接丢失或连接超时,对返回引用类型的 Web 服务的调用可能会返回 null。可以尝试重新建立连接,然后再次尝试调用。
可将你的应用状态还原到有效状态。 例如,你可能要执行多步骤的任务,该任务需要你在调用引发 NullReferenceException 的方法之前将信息保存到数据存储。 如果未初始化的对象可能会损坏数据记录,你可以在关闭应用之前删除之前的数据。
你需要报告异常。 例如,如果错误是由应用用户的错误引起的,则可以生成一条消息来帮助他们提供正确的信息。 你还可以记录有关该错误的信息来帮助你解决此问题。 某些框架(如 ASP.NET)具有高级别的异常处理程序,该处理程序可捕获所有错误,以便应用永远不会发生崩溃,在这种情况下,记录异常可能是你用来了解是否发生异常的唯一方式。
构造函数
NullReferenceException() |
初始化 类的新实例 NullReferenceException ,将新实例的 属性设置为 Message 系统提供的消息,该消息描述错误,例如“在需要对象的实例时找到了值'null'”。此消息考虑了当前系统区域性。 |
NullReferenceException(SerializationInfo, StreamingContext) |
已过时.
用序列化数据初始化 NullReferenceException 类的新实例。 |
NullReferenceException(String) |
用指定的错误消息初始化 NullReferenceException 类的新实例。 |
NullReferenceException(String, Exception) |
使用指定的错误消息和对作为此异常原因的内部异常的引用来初始化 NullReferenceException 类的新实例。 |
属性
Data |
获取键/值对的集合,这些键/值对提供有关该异常的其他用户定义信息。 (继承自 Exception) |
HelpLink |
获取或设置指向与此异常关联的帮助文件链接。 (继承自 Exception) |
HResult |
获取或设置 HRESULT(一个分配给特定异常的编码数字值)。 (继承自 Exception) |
InnerException |
获取导致当前异常的 Exception 实例。 (继承自 Exception) |
Message |
获取描述当前异常的消息。 (继承自 Exception) |
Source |
获取或设置导致错误的应用程序或对象的名称。 (继承自 Exception) |
StackTrace |
获取调用堆栈上的即时框架字符串表示形式。 (继承自 Exception) |
TargetSite |
获取引发当前异常的方法。 (继承自 Exception) |
方法
Equals(Object) |
确定指定对象是否等于当前对象。 (继承自 Object) |
GetBaseException() |
当在派生类中重写时,返回 Exception,它是一个或多个并发的异常的根本原因。 (继承自 Exception) |
GetHashCode() |
作为默认哈希函数。 (继承自 Object) |
GetObjectData(SerializationInfo, StreamingContext) |
已过时.
当在派生类中重写时,用关于异常的信息设置 SerializationInfo。 (继承自 Exception) |
GetType() |
获取当前实例的运行时类型。 (继承自 Exception) |
MemberwiseClone() |
创建当前 Object 的浅表副本。 (继承自 Object) |
ToString() |
创建并返回当前异常的字符串表示形式。 (继承自 Exception) |
事件
SerializeObjectState |
已过时.
当异常被序列化用来创建包含有关该异常的徐列出数据的异常状态对象时会出现该问题。 (继承自 Exception) |