什么是一个NullReferenceException,以及如何解决? [英] What is a NullReferenceException, and how do I fix it?

查看:156
本文介绍了什么是一个NullReferenceException,以及如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些code和执行时,它抛出一个的NullReferenceException 曰:

I have some code and when it executes, it throws a NullReferenceException, saying:

对象引用未设置到对象的实例。

Object reference not set to an instance of an object.

这是什么意思,和我能做些什么呢?

What does this mean, and what can I do about it?

推荐答案

您要使用的东西是(或没有在VB.NET)。这意味着你要么将其设置为,或者你从来没有将其设置为任何事情。

What is the cause?

Bottom Line

You are trying to use something that is null (or Nothing in VB.NET). This means you either set it to null, or you never set it to anything at all.

其他任何事情一样,被传来传去。如果是的方法A,这可能是因为方法B通过了的方法A。

Like anything else, null gets passed around. If it is null in method "A", it could be that method "B" passed a null to method "A".

本文的其余部分将详细,并显示错误,许多程序员往往使这可能会导致一个的NullReferenceException

The rest of this article goes into more detail and shows mistakes that many programmers often make which can lead to a NullReferenceException.

运行时抛出一个的NullReferenceException 总是意味着同样的事情:你正在尝试使用的参考。参考未初始化(或它的的初始化,但没有的的初始化)。

The runtime throwing a NullReferenceException always means the same thing: you are trying to use a reference. The reference is not initialized (or it was initialized, but is no longer initialized).

这意味着引用,你可以不通过引用访问的成员。最简单的例子:

This means the reference is null, and you cannot access members through a null reference. The simplest case:

string foo = null;
foo.ToUpper();

这将抛出一个的NullReferenceException 第二行,因为你不能调用实例方法 ToUpper的()字符串引用指向

This will throw a NullReferenceException at the second line, because you can't call the instance method ToUpper() on a string reference pointing to null.

你怎么找到的源的NullReferenceException ?除了看例外本身,这将正好在它出现的位置被抛出,在Visual Studio调试的一般规则:地方战略断点和的检查你的变量的,或者通过将鼠标悬停他们的名字,打开一个(快速)监视窗口或使用不同的调试面板像当地人和汽车。

How do you find the source of a NullReferenceException? Apart from looking at the exception itself, which will be thrown exactly at the location where it occurs, the general rules of debugging in Visual Studio apply: place strategic breakpoints and inspect your variables, either by hovering the mouse over their names, opening a (Quick)Watch window or using the various debugging panels like Locals and Autos.

如果你想找出给定值或没有设置,右键单击其名称并选择查​​找所有引用。然后,您可以放置​​一个断点在每个位置找到并附加调试器中运行您的程序。每一次这样的断点调试器符,你需要确定你是否希望引用非空,检查变量,并验证它的时候,你指望它指向一个实例。

If you want to find out where the reference is or isn't set, right-click its name and select "Find All References". You can then place a breakpoint at every found location and run your program with the debugger attached. Every time the debugger breaks on such a breakpoint, you need to determine whether you expect the reference to be non-null, inspect the variable and and verify that it points to an instance when you expect it to.

通过下面的程序流程这种方式,您可以在这里找到实例不能为空,为什么没有正确设置的位置。

By following the program flow this way you can find the location where the instance should not be null, and why it isn't properly set.

一些常见场景中的异常可能抛出:

Some common scenarios where the exception can be thrown:

ref1.ref2.ref3.member

如果REF1或REF2或REF3为空,那么你会得到一个的NullReferenceException 。如果要解决问题,然后找出哪一个是空通过改写前pression其简单等同的:

If ref1 or ref2 or ref3 is null, then you'll get a NullReferenceException. If you want to solve the problem, then find out which one is null by rewriting the expression to its simpler equivalent:

var r1 = ref1;
var r2 = r1.ref2;
var r3 = r2.ref3;
r3.member

具体而言,在 HttpContext.Current.User.Identity.Name HttpContext.Current 可能为空,或的 User属性可能为空,或身份属性可以为空。

Specifically, in HttpContext.Current.User.Identity.Name, the HttpContext.Current could be null, or the User property could be null, or the Identity property could be null.

public class Person {
    public int Age { get; set; }
}
public class Book {
    public Person Author { get; set; }
}
public class Example {
    public void Foo() {
        Book b1 = new Book();
        int authorAge = b1.Author.Age; // You never initialized the Author property.
                                       // there is no Person to get an Age from.
    }
}

这同样适用于嵌套对象初始化:

The same applies to nested object initializers:

Book b1 = new Book { Author = { Age = 45 } };

虽然使用了关键字,它仅创建图书的新实例,而不是一个新的实例人的,所以作者属性仍然是

While the new keyword is used, it only creates a new instance of Book, but not a new instance of Person, so the Author the property is still null.

int[] numbers = null;
int n = numbers[0]; // numbers is null. There is no array to index.

数组元素

Person[] people = new Person[5];
people[0].Age = 20 // people[0] is null. The array was allocated but not
                   // initialized. There is no Person to set the Age for.

铁血阵列

long[][] array = new long[1][];
array[0][0] = 3; // is null because only the first dimension is yet initialized.
                 // Use array[0] = new long[2]; first.

收藏/列表/字典

Dictionary<string, int> agesForNames = null;
int age = agesForNames["Bob"]; // agesForNames is null.
                               // There is no Dictionary to perform the lookup.

范围变量(间接/递延)

public class Person {
    public string Name { get; set; }
}
var people = new List<Person>();
people.Add(null);
var names = from p in people select p.Name;
string firstName = names.First(); // Exception is thrown here, but actually occurs
                                  // on the line above.  "p" is null because the
                                  // first element we added to the list is null.

活动

public class Demo
{
    public event EventHandler StateChanged;

    protected virtual void OnStateChanged(EventArgs e)
    {        
        StateChanged(this, e); // Exception is thrown here 
                               // if no event handlers have been attached
                               // to StateChanged event
    }
}

坏命名约定:

如果你从不同的当地人命名字段,你可能已经意识到,你从来没有初始化的领域。

Bad Naming Conventions:

If you named fields differently from locals, you might have realized that you never initialized the field.

public class Form1 {
    private Customer customer;

    private void Form1_Load(object sender, EventArgs e) {
        Customer customer = new Customer();
        customer.Name = "John";
    }

    private void Button_Click(object sender, EventArgs e) {
        MessageBox.Show(customer.Name);
    }
}

这可以通过用下划线按照惯例来preFIX领域需要解决:

This can be solved by following the convention to prefix fields with an underscore:

private Customer _customer;

ASP.NET页面生命周期:

public partial class Issues_Edit : System.Web.UI.Page
{
    protected TestIssue myIssue;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            // Only called on first load, not when button clicked
            myIssue = new TestIssue(); 
        }
    }

    protected void SaveButton_Click(object sender, EventArgs e)
    {
        myIssue.Entry = "NullReferenceException here!";
    }
}

ASP.NET会话值

// if the "FirstName" session value has not yet been set,
// then this line will throw a NullReferenceException
string firstName = Session["FirstName"].ToString();

ASP.NET MVC空视图模型

如果引用在ASP.NET MVC视图 @Model 的属性时出现异常,你需要明白,模式获取你的操作方法设置,当你收益视图。当您从控制器返回一个空模型(或模型属性),当意见访问它出现异常:

ASP.NET MVC empty view models

If the exception occurs when referencing a property of @Model in an ASP.NET MVC view, you need to understand that the Model gets set in your action method, when you return a view. When you return an empty model (or model property) from your controller, the exception occurs when the views accesses it:

// Controller
public class Restaurant:Controller
{
    public ActionResult Search()
    {
         return View();  // Forgot the provide a Model here.
    }
}

// Razor view 
@foreach (var restaurantSearch in Model.RestaurantSearch)  // Throws.
{
}

<p>@Model.somePropertyName</p> <!-- Also throws -->

WPF控件创建顺序和活动

在它们出现在可视化树的顺序调用的InitializeComponent 期间创建

WPF控件。 A 的NullReferenceException 将与事件处理程序等,火灾早期创建的控件的情况下,在的InitializeComponent 提高其参考后期创建的控件。

WPF Control Creation Order and Events

WPF controls are created during the call to InitializeComponent in the order they appear in the visual tree. A NullReferenceException will be raised in the case of early-created controls with event handlers, etc, that fire during InitializeComponent which reference late-created controls.

例如:

<Grid>
    <!-- Combobox declared first -->
    <ComboBox Name="comboBox1" 
              Margin="10"
              SelectedIndex="0" 
              SelectionChanged="comboBox1_SelectionChanged">
        <ComboBoxItem Content="Item 1" />
        <ComboBoxItem Content="Item 2" />
        <ComboBoxItem Content="Item 3" />
    </ComboBox>

    <!-- Label declared later -->
    <Label Name="label1" 
           Content="Label"
           Margin="10" />
</Grid>

下面 comboBox1 之前卷标1 创建。如果 comboBox1_SelectionChanged 试图引用卷标1 它暂时还不能被创建。

Here comboBox1 is created before label1. If comboBox1_SelectionChanged attempts to reference label1 it will not yet have been created.

private void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    label1.Content = comboBox1.SelectedIndex.ToString(); // NullReference here!!
}

在XAML改变声明的顺序(即:上市卷标1 comboBox1 ,设计忽视的问题哲学,至少会解决的NullReferenceException 在这里。

Changing the order of the declarations in the XAML (ie: listing label1 before comboBox1, ignoring issues of design philosophy, would at least resolve the NullReferenceException here.

var myThing = someObject as Thing;

这不会抛出一个InvalidCastException,但返回时投失败(当someObject本身就是空)。所以,要意识到这一点。

This doesn't throw an InvalidCastException, but returns a null when the cast fails (and when someObject is itself null). So be aware of that.

平原版本第一()单()抛出异常的时候没有什么。在OrDefault版本,在这种情况下返回null。所以,要意识到这一点。

The plain versions First() and Single() throw exceptions when there is nothing. The "OrDefault" versions return null in that case. So be aware of that.

如果您希望引用有时为空,您可以检查它是访问实例成员之前:

If you expect the reference sometimes to be null, you can check for it being null before accessing instance members:

void PrintName(Person p) {
    if (p != null) {
        Console.WriteLine(p.Name);
    }
}

显式检查,并提供一个默认值。

方法的调用,您希望返回一个实例可以返回,例如,当受到追捧对象不能被发现。您可以选择返回默认值时,是这样的话:

Explicitly check for null, and provide a default value.

Methods calls you expect to return an instance can return null, for example when the object being sought cannot be found. You can choose to return a default value when this is the case:

string GetCategory(Book b) {
    if (b == null)
        return "Unknown";
    return b.Category;
}

明确地方法调用检查,并抛出一个自定义异常。

您也可以引发自定义异常,只有抓住它在调用code:

Explicitly check for null from method calls and throw a custom exception.

You can also throw a custom exception, only to catch it in the calling code:

string GetCategory(string bookTitle) {
    var book = library.FindBook(bookTitle);  // This may return null
    if (book == null)
        throw new BookNotFoundException(bookTitle);  // Your custom exception
    return book.Category;
}

使用 Debug.Assert的如果某个值不应该,不是例外发生及早发现问题

在开发过程中知道的方法也许可以,但实际上不应该返回,您可以使用 Debug.Assert的()来尽快当它发生断裂:

Use Debug.Assert if a value should never be null, to catch the problem earlier than the exception occurs.

When you know during development that a method maybe can, but actually never should return null, you can use Debug.Assert() to break as soon as possible when it does occur:

string GetTitle(int knownBookID) {
    // You know this should never return null.
    var book = library.GetBook(knownBookID);  

    // Exception will occur on the next line instead of at the end of this method.
    Debug.Assert(book != null, "Library didn't return a book for known book ID.");

    // Some other code ...

    return book.Title; // Will never throw NullReferenceException in Debug mode.
}

虽然这个检查不会在发布版本结束了,造成它扔的NullReferenceException 时再书== NULL 在释放模式运行。

Though this check will not end up in your release build, causing it to throw the NullReferenceException again when book == null at runtime in release mode.

DateTime? appointment = null;
Console.WriteLine(appointment.GetValueOrDefault(DateTime.Now));
// Will display the default value provided (DateTime.Now), because appointment is null.

appointment = new DateTime(2022, 10, 20);
Console.WriteLine(appointment.GetValueOrDefault(DateTime.Now));
// Will display the appointment date, not the default

使用空合并运算符: ?? [C#]或如果() [VB]

速记提供一个默认值时,遇到:

Use the null coalescing operator: ?? [C#] or If() [VB].

The shorthand to providing a default value when a null is encountered:

IService CreateService(ILogger log, Int32? frobPowerLevel)
{
    var serviceImpl = new MyService(log ?? NullLog.Instance);

    // Note that the above "GetValueOrDefault()" can also be rewritten to use
    // the coalesce operator:
    serviceImpl.FrobPowerLevel = frobPowerLevel ?? 5;
}

使用空条件运算符: (在C#6和VB.NET 14可用):

这是有时也称为安全航行或猫王(其形状后)运营商。如果运营商的左侧的前pression为null,则右侧将不会被计算,转而返回null。这意味着情况是这样的:

Use the null condition operator: ?. (available in C# 6 and VB.NET 14):

This is also sometimes called the safe navigation or Elvis (after its shape) operator. If the expression on the left side of the operator is null, then the right side will not be evaluated and null is returned instead. That means cases like this:

var title = person.Title.ToUpper();

如果此人没有一个冠军,这将是因为它试图调用 ToUpper的与空值的属性抛出异常。

If the person does not have a title, this will throw an exception because it is trying to call ToUpper on a property with a null value.

在C#5以下,这可与守卫:

In C# 5 and below, this can be guarded with:

var title = person.Title == null ? null : person.Title.ToUpper();

现在标题变量将是无效的,而不是抛出异常。 C#6引入了一个较短的语法是:

Now the title variable will be null instead of throwing an exception. C# 6 introduces a shorter syntax for this:

var title = person.Title?.ToUpper();

这将导致在标题变量被空,并调用ToUpper的不发,如​​果 person.Title 为null。

This will result in the title variable being null, and the call to ToUpper is not made if person.Title is null.

当然,你的还是的必须检查标题为空或空合并运算符(<$ C一起使用空条件运算符$ C> ?? )提供一个默认值:

Of course, you still have to check title for null or use the null condition operator together with the null coalescing operator (??) to supply a default value:

// regular null check
int titleLength = 0;
if (title != null)
    titleLength = title.Length; // If title is null, this would throw NullReferenceException

// combining the `?` and the `??` operator
int titleLength = title?.Length ?? 0;

这篇关于什么是一个NullReferenceException,以及如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆