什么是NullReferenceException,我该如何修复?

发布于 2025-01-17 18:31:01 字数 144 浏览 6 评论 0 原文

我有一些代码,当它执行时,它会引发 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 to fix this error?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(26

蓝海 2025-01-24 18:31:01

什么原因?

底线

您正在尝试使用 null (或 nothing 中的)的东西。这意味着您要么将其设置为 null ,要么根本不会将其设置为任何东西。

像其他任何东西一样, null 被传递。如果是 null 方法“ a”,则可以是该方法“ b”传递 null to 方法“ A”。

null 可以具有不同的含义:

  1. 的对象变量,因此 noth noth。在这种情况下,如果您访问此类对象的成员,它导致 nullReferenceException
  2. 开发人员使用 null 有意表示没有有意义的值。请注意,C#具有用于变量的无效数据型的概念(例如,数据库表可以具有不可用字段) - 您可以将 null 分配给他们,以表明其中没有存储的值,例如 int? a = null; (这是 nullable< int> a = null; )的快捷方式,其中问号表示它可以存储在变量中 null a 。如果(a.hasvalue){...} 或 if(a == null){...} ,您可以使用进行检查。无效的变量,例如 a 在此示例中,允许通过 a.value 明确或通过 a 明确访问该值。
    Note 通过 A.Value 访问 InvalidOperationException 而不是 NullReferenceException 如果 ,则将其访问它。 is null - 您应该事先进行检查,即,如果您有另一个不可用的变量 int b; ,则应进行分配,例如 if(a.hasvalue){b = a.value; } 或shorter if(a!= null){b = a; }

本文的其余部分详细介绍了许多程序员经常犯的错误,这可能会导致 nullReferenceException

更具体地说的是

Runtime 投掷 nullReferenceException 始终表示同一件事:您正在尝试使用参考,并且该参考未初始化(或它是 初始化的,但不再是 初始化)。

这意味着引用为 null ,并且您无法通过 null 参考访问成员(例如方法)。最简单的情况:

string foo = null;
foo.ToUpper();

这将在第二行上扔 nullReferenceException ,因为您无法在 string> String Refere指向 null

调试

如何找到 nullReferenceException 的来源?除了查看异常本身(将完全在发生的位置)上抛出外,在Visual Studio中调试的一般规则适用:放置战略断点和检查变量,要么通过将鼠标悬停在其名称上,打开(快速)手表窗口,或使用当地人等各种调试面板,例如汽车。

如果您想找出引用所在或未设置的位置,请右键单击其名称,然后选择“查找所有参考”。然后,您可以在每个发现的位置放置一个断点,并使用附加的调试器运行程序。每当调试器在此类断点上断裂时,您都需要确定您期望参考是否为非零,检查变量,并验证它在期望时指向一个实例。

通过以这种方式遵循程序流,您可以找到该实例不应为null的位置,以及为什么未正确设置该实例。

示例

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

如果Ref1或Ref2或Ref3为

ref1.ref2.ref3.member

null,则您将获得 nullReferenceException 。如果要解决问题,请通过将表达式重写为更简单的等效词来找出null:

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

具体来说,在 httpcontext.current.current.user.sideity.name.name 中,>>>> httpcontext。当前可以为null,或者用户属性可以为null,或者 Identity 属性可以为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.
    }
}

如果您想避免孩子(人)null引用,则可以在父(书)对象的构造函数中初始化它。

嵌套对象初始化器

相同适用于嵌套对象初始化器:

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

这转化为:

Book b1 = new Book();
b1.Author.Age = 45;

new 使用关键字,它仅创建一个新实例 book ,但不创建新实例 person 的内容,因此作者属性仍然是 null

嵌套集合初始化

public class Person 
{
    public ICollection<Book> Books { get; set; }
}
public class Book 
{
    public string Title { get; set; }
}

嵌套集合 initializers 表现相同:

Person p1 = new Person 
{
    Books = {
         new Book { Title = "Title1" },
         new Book { Title = "Title2" },
    }
};

这转化为:

Person p1 = new Person();
p1.Books.Add(new Book { Title = "Title1" });
p1.Books.Add(new Book { Title = "Title2" });

新人仅创建一个 person 的实例,但是书籍集合仍然是 null 。集合 initializer 语法不创建集合
对于 p1.books ,它仅转化为 p1.books.add(...)语句。

阵列

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.

事件(C#)

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
    }
}

(注意:VB.NET编译器插入事件使用情况的null检查,因此不必检查 在vb.net中。)

不良命名约定:

如果您以不同于当地人的名字命名字段,则可能已经意识到您从未初始化该字段。

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);
    }
}

可以通过遵循公约以下划线的前缀字段来解决:

    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空视图模型

如果在引用 @model的属性时发生异常,则代码>在 asp.net MVC视图中,您需要了解 model 在您的操作方法中设置时,当您 return> return a视图时。当您从控制器返回空模型(或模型属性)时,当视图访问视图时发生异常:

// 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控制创建订单和事件

wpf 控件是在调用 indiralizecomponent ointerizecomponent < /代码>按顺序出现在视觉树中。 nullReferenceException 将在带有事件处理程序等的早期创建控件等的情况下提高,该控件在 initizecomponent 中发射,其中引用晚期创建的控件。

例如:

<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 是在 label1 之前创建的。如果 combobox1_selectionChanged 尝试引用 label1 ,则尚未创建它。

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

XAML 中更改声明的顺序(即,列表 label1 combobox1 之前,忽略设计理念的问题)至少可以解决<代码> NullReferenceException 在此处。

用铸造

var myThing = someObject as Thing;

这不会抛出 invalidcastException ,但在铸件失败时返回 null (以及当 someobject 时)本身为null)。因此请注意。

linq firstordEfault() singleordefault()

普通版本什么都没有。在这种情况下,“ ORDEFAULT”版本返回 null 。因此请注意。

foreach

foreach 尝试在 null 集合上迭代时投掷。通常是由意外 null 引起的,由返回集合的方法产生。

List<int> list = null;    
foreach(var v in list) { } // NullReferenceException here

更现实的示例 - 从XML文档中选择节点。如果找不到节点,则会投掷,但初始调试显示所有属性有效:

foreach (var node in myData.MyXml.DocumentNode.SelectNodes("//Data"))

的方法

避免明确检查 null 并忽略 null

。如果您希望参考有时为 null ,则可以在访问实例成员之前检查它是 null

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

明确检查 null 并提供一个默认值。

您调用的方法期望一个实例可以返回 null ,例如找不到目标时。在这种情况下,您可以选择返回默认值:

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

从方法调用中明确检查 null ,然后抛出自定义异常。

您还可以抛出自定义异常,只能在调用代码中捕获它:

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 ,如果一个值不应为 null ,则比早于问题发生例外。

当您在开发过程中知道一种方法可以,但永远不要返回 null 时,可以使用 debug.assert()在发生时尽快破裂:

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 再次在发行模式下运行时 book == null

使用 getValueordEfault() nullable 值类型在 null 时提供默认值类型。

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

使用NULL合并运算符: ?? [C#]或 if() [vb]。

遇到 null 时提供默认值的速记:

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;
}

使用null条件运算符:?。可在C#6和VB.NET 14中提供:

有时也称为安全导航或猫王(其形状后)操作员。如果操作员左侧的表达式为null,则将无法评估右侧,而返回空。这意味着这样的案例:

var title = person.Title.ToUpper();

如果该人没有标题,则会引发异常,因为它试图在具有无效值的属性上调用 toupper

c#5 及以下,可以对此进行防护:

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

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

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

这将导致标题变量为 null ,并且如果 person.title null

当然,您仍然必须检查 null 或将NULL条件运算符与Null Colescing Operator( ?? )要提供默认值:

// 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;

同样,对于数组,您可以使用?[i] 如下:

int[] myIntArray = null;
var i = 5;
int? elem = myIntArray?[i];
if (!elem.HasValue) Console.WriteLine("No value");

这将执行以下操作:如果 myintArray null ,表达式返回 null ,您可以安全地检查它。如果包含一个数组,它将执行与:
elem = myintArray [i]; 并返回i th 元素。

使用NULL上下文(在C#8中可用):

c#8 中引入,无效的上下文和Nullable参考类型在变量上执行静态分析,并提供编译器警告,如果值可能是 null 或已设置为 null 。无效的参考类型允许明确允许类型为 null

可以使用 csproj 文件中的 nullable 元素为项目设置可确定的注释上下文和无效的警告上下文。此元素配置编译器如何解释类​​型的无效性和生成哪些警告。有效的设置为:

  • 启用:启用了无效的注释上下文。启用了无效的警告上下文。例如,引用类型的变量,例如,字符串是不可删除的。所有无效警告均已启用。
  • 禁用:禁用了无效的注释上下文。无效的警告上下文被禁用。就像c#的早期版本一样,参考类型的变量是遗忘的。所有无效警告均被禁用。
  • safeonly :启用了无效的注释上下文。无效的警告环境是安全的。参考类型的变量是不可删除的。启用了所有安全性无性警告。
  • 警告:禁用了无效的注释上下文。启用了无效的警告上下文。参考类型的变量忽略了。所有无效警告均已启用。
  • safeonlywarnings :禁用了无效的注释上下文。无效的警告环境是安全的。
    参考类型的变量忽略了。启用了所有安全性无性警告。

使用与无效的值类型相同的语法记录了一个无效的参考类型:将附加到变量的类型上。

用于调试和修复迭代器中的null derefs的特殊技术

c#支持“迭代器块”(在其他一些流行语言中称为“发电机”)。 nullReferenceException 由于执行延迟,在迭代器块中调试可能特别棘手:

public IEnumerable<Frob> GetFrobs(FrobFactory f, int count)
{
    for (int i = 0; i < count; ++i)
    yield return f.MakeFrob();
}
...
FrobFactory factory = whatever;
IEnumerable<Frobs> frobs = GetFrobs();
...
foreach(Frob frob in frobs) { ... }

如果 whything 导致 null null 然后 makefrob 会扔。现在,您可能会认为正确的做法是:

// DON'T DO THIS
public IEnumerable<Frob> GetFrobs(FrobFactory f, int count)
{
   if (f == null) 
      throw new ArgumentNullException("f", "factory must not be null");
   for (int i = 0; i < count; ++i)
      yield return f.MakeFrob();
}

为什么这是错误的?因为迭代器块实际上没有运行,直到 foreach !调用 getfrobs 只需返回一个iTerated 时将运行迭代器块的对象。

通过编写 null 以下检查,您可以防止 nullReferenceException ,但是您将 nullargumentException 移动到 iteration ,不是 call 的点,这是非常混乱的调试

正确的修复程序是:

// DO THIS
public IEnumerable<Frob> GetFrobs(FrobFactory f, int count)
{
   // No yields in a public method that throws!
   if (f == null) 
       throw new ArgumentNullException("f", "factory must not be null");
   return GetFrobsForReal(f, count);
}
private IEnumerable<Frob> GetFrobsForReal(FrobFactory f, int count)
{
   // Yields in a private method
   Debug.Assert(f != null);
   for (int i = 0; i < count; ++i)
        yield return f.MakeFrob();
}

也就是说,制作具有迭代器块逻辑和公共表面方法的私人助手方法,该方法可以执行 null 检查并返回迭代器。现在,当调用 getfrobs 时, null 检查立即发生,然后 getfrobsforreal 在迭代序列时执行。

如果将 linq 的参考源检查到对象中,您将看到此技术在整个过程中都使用。编写稍微笨拙,但使调试无效错误变得容易得多。 为了方便呼叫者而不是作者的便利性,优化您的代码

关于不安全代码 c#c​​#的无效代码的注释,

其“不安全”模式,顾名思义,这是极其危险的,因为提供记忆安全性和类型安全性的正常安全机制未能强制执行。 ,除非您对记忆的工作原理有深刻而深刻的了解,否则您不应编写不安全的代码

在不安全的模式下,您应该意识到两个重要的事实:

  • 取消定位指针产生的例外与删除null 参考
  • 降低无效的非零指针在某些情况下可以

理解为什么会产生该例外,它有助于了解.NET首先如何产生 nullReferenceException 。 (这些详细信息适用于在Windows上运行的.NET;其他操作系统使用相似的机制。)

内存在 Windows 中虚拟化;每个过程都会获得操作系统跟踪的许多内存“页面”的虚拟内存空间。内存的每个页面都设置了标志,以确定其使用方式:从,写入,执行等。 最低页面被标记为“如果以任何方式使用,则产生错误”。

c#中的空指针和null引用均以内部表示为零,因此任何尝试将其放在相应的内存存储中的尝试都会导致操作系统产生错误。然后,.NET运行时检测此错误,并将其转换为 nullReferenceException

这就是为什么删除无效指针和无效参考会产生相同例外的原因。

第二点呢?删除属于虚拟内存最低页面的任何无效指针都会导致相同的操作系统错误,从而同样的例外。

为什么这有意义?好吧,假设我们有一个包含两个INT的结构,一个不受托管的指针等于NULL。如果我们试图放置结构中的第二个int,则 clr 将不会尝试访问位置零的存储;它将在位置四处访问存储空间。但是从逻辑上讲,这是一个无效的解雇,因为我们正在通过 null到达该地址

如果您正在使用不安全的代码,并且会得到 nullReferenceException ,请注意,有问题的指针不必为null。它可以是最低页面中的任何位置,并且将产生此例外。

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.

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".

null can have different meanings:

  1. Object variables that are uninitialized and hence point to nothing. In this case, if you access members of such objects, it causes a NullReferenceException.
  2. The developer is using null intentionally to indicate there is no meaningful value available. Note that C# has the concept of nullable datatypes for variables (like database tables can have nullable fields) - you can assign null to them to indicate there is no value stored in it, for example int? a = null; (which is a shortcut for Nullable<int> a = null;) where the question mark indicates it is allowed to store null in variable a. You can check that either with if (a.HasValue) {...} or with if (a==null) {...}. Nullable variables, like a in this example, allow to access the value via a.Value explicitly, or just as normal via a.
    Note that accessing it via a.Value throws an InvalidOperationException instead of a NullReferenceException if a is null - you should do the check beforehand, i.e. if you have another non-nullable variable int b; then you should do assignments like if (a.HasValue) { b = a.Value; } or shorter if (a != null) { b = a; }.

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

More Specifically

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

This means the reference is null, and you cannot access members (such as methods) through a null reference. The simplest case:

string foo = null;
foo.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.

Debugging

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 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.

Examples

Some common scenarios where the exception can be thrown:

Generic

ref1.ref2.ref3.member

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

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.

Indirect

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.
    }
}

If you want to avoid the child (Person) null reference, you could initialize it in the parent (Book) object's constructor.

Nested Object Initializers

The same applies to nested object initializers:

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

This translates to:

Book b1 = new Book();
b1.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 property is still null.

Nested Collection Initializers

public class Person 
{
    public ICollection<Book> Books { get; set; }
}
public class Book 
{
    public string Title { get; set; }
}

The nested collection Initializers behave the same:

Person p1 = new Person 
{
    Books = {
         new Book { Title = "Title1" },
         new Book { Title = "Title2" },
    }
};

This translates to:

Person p1 = new Person();
p1.Books.Add(new Book { Title = "Title1" });
p1.Books.Add(new Book { Title = "Title2" });

The new Person only creates an instance of Person, but the Books collection is still null. The collection Initializer syntax does not create a collection
for p1.Books, it only translates to the p1.Books.Add(...) statements.

Array

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

Array Elements

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.

Jagged Arrays

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.

Collection/List/Dictionary

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

Range Variable (Indirect/Deferred)

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.

Events (C#)

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
    }
}

(Note: The VB.NET compiler inserts null checks for event usage, so it's not necessary to check events for Nothing in VB.NET.)

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);
    }
}

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

    private Customer _customer;

ASP.NET Page Life cycle:

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 Session Values

// 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 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 access 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 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.

For example:

<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>

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(); // NullReferenceException here!!
}

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

Cast with as

var myThing = someObject as Thing;

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.

LINQ FirstOrDefault() and SingleOrDefault()

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.

foreach

foreach throws when you try to iterate on a null collection. Usually caused by unexpected null result from methods that return collections.

List<int> list = null;    
foreach(var v in list) { } // NullReferenceException here

More realistic example - select nodes from XML document. Will throw if nodes are not found but initial debugging shows that all properties valid:

foreach (var node in myData.MyXml.DocumentNode.SelectNodes("//Data"))

Ways to Avoid

Explicitly check for null and ignore null values.

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 you call expecting 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;
}

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;
}

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 could, but 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.
}

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.

Use GetValueOrDefault() for nullable value types to provide a default value when they are null.

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

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;
}

Use the null condition operator: ?. or ?[x] for arrays (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();

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.

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

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

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();

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

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;

Likewise, for arrays you can use ?[i] as follows:

int[] myIntArray = null;
var i = 5;
int? elem = myIntArray?[i];
if (!elem.HasValue) Console.WriteLine("No value");

This will do the following: If myIntArray is null, the expression returns null and you can safely check it. If it contains an array, it will do the same as:
elem = myIntArray[i]; and returns the ith element.

Use null context (available in C# 8):

Introduced in C# 8, null contexts and nullable reference types perform static analysis on variables and provide a compiler warning if a value can be potentially null or have been set to null. The nullable reference types allow types to be explicitly allowed to be null.

The nullable annotation context and nullable warning context can be set for a project using the Nullable element in your csproj file. This element configures how the compiler interprets the nullability of types and what warnings are generated. Valid settings are:

  • enable: The nullable annotation context is enabled. The nullable warning context is enabled. Variables of a reference type, string, for example, are non-nullable. All nullability warnings are enabled.
  • disable: The nullable annotation context is disabled. The nullable warning context is disabled. Variables of a reference type are oblivious, just like earlier versions of C#. All nullability warnings are disabled.
  • safeonly: The nullable annotation context is enabled. The nullable warning context is safeonly. Variables of a reference type are non-nullable. All safety nullability warnings are enabled.
  • warnings: The nullable annotation context is disabled. The nullable warning context is enabled. Variables of a reference type are oblivious. All nullability warnings are enabled.
  • safeonlywarnings: The nullable annotation context is disabled. The nullable warning context is safeonly.
    Variables of a reference type are oblivious. All safety nullability warnings are enabled.

A nullable reference type is noted using the same syntax as nullable value types: a ? is appended to the type of the variable.

Special techniques for debugging and fixing null derefs in iterators

C# supports "iterator blocks" (called "generators" in some other popular languages). NullReferenceException can be particularly tricky to debug in iterator blocks because of deferred execution:

public IEnumerable<Frob> GetFrobs(FrobFactory f, int count)
{
    for (int i = 0; i < count; ++i)
    yield return f.MakeFrob();
}
...
FrobFactory factory = whatever;
IEnumerable<Frobs> frobs = GetFrobs();
...
foreach(Frob frob in frobs) { ... }

If whatever results in null then MakeFrob will throw. Now, you might think that the right thing to do is this:

// DON'T DO THIS
public IEnumerable<Frob> GetFrobs(FrobFactory f, int count)
{
   if (f == null) 
      throw new ArgumentNullException("f", "factory must not be null");
   for (int i = 0; i < count; ++i)
      yield return f.MakeFrob();
}

Why is this wrong? Because the iterator block does not actually run until the foreach! The call to GetFrobs simply returns an object which when iterated will run the iterator block.

By writing a null check like this you prevent the NullReferenceException, but you move the NullArgumentException to the point of the iteration, not to the point of the call, and that is very confusing to debug.

The correct fix is:

// DO THIS
public IEnumerable<Frob> GetFrobs(FrobFactory f, int count)
{
   // No yields in a public method that throws!
   if (f == null) 
       throw new ArgumentNullException("f", "factory must not be null");
   return GetFrobsForReal(f, count);
}
private IEnumerable<Frob> GetFrobsForReal(FrobFactory f, int count)
{
   // Yields in a private method
   Debug.Assert(f != null);
   for (int i = 0; i < count; ++i)
        yield return f.MakeFrob();
}

That is, make a private helper method that has the iterator block logic and a public surface method that does the null check and returns the iterator. Now when GetFrobs is called, the null check happens immediately, and then GetFrobsForReal executes when the sequence is iterated.

If you examine the reference source for LINQ to Objects you will see that this technique is used throughout. It is slightly more clunky to write, but it makes debugging nullity errors much easier. Optimize your code for the convenience of the caller, not the convenience of the author.

A note on null dereferences in unsafe code

C# has an "unsafe" mode which is, as the name implies, extremely dangerous because the normal safety mechanisms which provide memory safety and type safety are not enforced. You should not be writing unsafe code unless you have a thorough and deep understanding of how memory works.

In unsafe mode, you should be aware of two important facts:

  • dereferencing a null pointer produces the same exception as dereferencing a null reference
  • dereferencing an invalid non-null pointer can produce that exception in some circumstances

To understand why that is, it helps to understand how .NET produces NullReferenceException in the first place. (These details apply to .NET running on Windows; other operating systems use similar mechanisms.)

Memory is virtualized in Windows; each process gets a virtual memory space of many "pages" of memory that are tracked by the operating system. Each page of memory has flags set on it that determine how it may be used: read from, written to, executed, and so on. The lowest page is marked as "produce an error if ever used in any way".

Both a null pointer and a null reference in C# are internally represented as the number zero, and so any attempt to dereference it into its corresponding memory storage causes the operating system to produce an error. The .NET runtime then detects this error and turns it into the NullReferenceException.

That's why dereferencing both a null pointer and a null reference produces the same exception.

What about the second point? Dereferencing any invalid pointer that falls in the lowest page of virtual memory causes the same operating system error, and thereby the same exception.

Why does this make sense? Well, suppose we have a struct containing two ints, and an unmanaged pointer equal to null. If we attempt to dereference the second int in the struct, the CLR will not attempt to access the storage at location zero; it will access the storage at location four. But logically this is a null dereference because we are getting to that address via the null.

If you are working with unsafe code and you get a NullReferenceException, just be aware that the offending pointer need not be null. It can be any location in the lowest page, and this exception will be produced.

妖妓 2025-01-24 18:31:01

NullReference 异常 - Visual Basic

Visual BasicNullReference 异常C# 中的 NullReference 异常没有什么不同。毕竟,它们都报告它们都使用的 .NET Framework 中定义的相同异常。 Visual Basic 特有的原因很少(也许只有一个)。

本答案将使用 Visual Basic 术语、语法和上下文。使用的例子来自大量过去的Stack溢出问题。这是为了通过使用帖子中常见的各种情况来最大化相关性。还为可能需要的人提供了更多解释。此处很可能列出了与您类似的示例。

注意:

  1. 这是基于概念的:没有代码可供您粘贴到项目中。它旨在帮助您了解导致 NullReferenceException (NRE) 的原因、如何找到它、如何修复它以及如何避免它。 NRE 可能由多种原因引起,因此这不太可能是您唯一遇到的情况。
  2. 这些示例(来自 Stack Overflow 帖子)并不总是首先展示做某事的最佳方法。
  3. 通常,使用最简单的补救措施。

基本含义

消息“对象未设置为对象的实例”意味着您正在尝试使用尚未初始化的对象。这可以归结为以下之一:

  • 您的代码声明了一个对象变量,但没有初始化它(创建一个实例或“实例化”)它)
  • 您的代码假设会初始化对象的东西,但没有
  • 可能,其他代码过早地使仍在使用的对象失效

查找原因

由于问题是一个对象引用,它是Nothing,所以答案是检查它们以找出哪些 一。然后判断为什么没有初始化。将鼠标悬停在各个变量上,Visual Studio (VS) 将显示它们的值 - 罪魁祸首将是 Nothing

IDE debug display

您还应该从相关代码中删除所有 Try/Catch 块,尤其是 Catch 中没有任何内容的代码块堵塞。当您的代码尝试使用 Nothing 的对象时,这将导致您的代码崩溃。 这就是您想要的,因为它将识别问题的确切位置,并允许您识别导致问题的对象。

Catch 中显示 Error while...MsgBox 没有什么帮助。此方法还会导致堆栈非常糟糕溢出问题,因为您无法描述实际的异常、涉及的对象甚至发生异常的代码行。

您还可以使用Locals Window调试 -> Windows -> Locals)来检查您的对象。

一旦您知道问题是什么以及问题出在哪里,通常就很容易修复,并且比发布新问题更快。

另请参阅:

示例和补救措施

类对象/创建实例

Dim reg As CashRegister
...
TextBox1.Text = reg.Amount         ' NRE

问题是 Dim 不会创建 CashRegister 对象;它仅声明该类型的名为 reg 的变量。 声明对象变量和创建实例是两件不同的事情。

补救措施

New 运算符通常可用于在声明实例时创建实例:

Dim reg As New CashRegister        ' [New] creates instance, invokes the constructor

' Longer, more explicit form:
Dim reg As CashRegister = New CashRegister

当仅适合稍后创建实例时:

Private reg As CashRegister         ' Declare
  ...
reg = New CashRegister()            ' Create instance

注意:不要< /strong> 在过程中再次使用 Dim,包括构造函数 (Sub New):

Private reg As CashRegister
'...

Public Sub New()
   '...
   Dim reg As New CashRegister
End Sub

这将创建一个本地变量,reg ,仅存在于该上下文(子)中。您将在其他地方使用的具有模块级别 Scopereg 变量仍然是 Nothing

缺少 New 运算符是堆栈中出现 NullReference 异常 的第一大原因审查了溢出问题。

Visual Basic 尝试使用 New 反复使该过程变得清晰:使用 New 运算符创建一个 < strong>new 对象并调用 Sub New - 构造函数 - 您的对象可以在其中执行任何其他初始化。

需要明确的是,Dim(或Private)仅声明一个变量及其类型。变量的范围 - 无论它存在于整个模块/类还是局部于过程 - 由声明它的位置决定。 私人 |朋友| Public 定义访问级别,而不是范围

有关详细信息,请参阅:


数组

数组还必须实例化:

Private arr as String()

该数组仅被声明,尚未创建。初始化数组的方法有多种:

Private arr as String() = New String(10){}
' or
Private arr() As String = New String(10){}

' For a local array (in a procedure) and using 'Option Infer':
Dim arr = New String(10) {}

注意:从 VS 2010 开始,当使用文字和 Option Infer 初始化本地数组时,As 和 < code>New 元素是可选的:

Dim myDbl As Double() = {1.5, 2, 9.9, 18, 3.14}
Dim myDbl = New Double() {1.5, 2, 9.9, 18, 3.14}
Dim myDbl() = {1.5, 2, 9.9, 18, 3.14}

数据类型和数组大小是根据分配的数据推断出来的。类/模块级别声明仍然需要 As Option Strict

Private myDoubles As Double() = {1.5, 2, 9.9, 18, 3.14}

示例:类对象数组

Dim arrFoo(5) As Foo

For i As Integer = 0 To arrFoo.Count - 1
   arrFoo(i).Bar = i * 10       ' Exception
Next

数组已创建,但是其中的 Foo 对象还没有。

补救措施

For i As Integer = 0 To arrFoo.Count - 1
    arrFoo(i) = New Foo()         ' Create Foo instance
    arrFoo(i).Bar = i * 10
Next

使用 List(Of T) 会使元素没有有效对象变得非常困难:

Dim FooList As New List(Of Foo)     ' List created, but it is empty
Dim f As Foo                        ' Temporary variable for the loop

For i As Integer = 0 To 5
    f = New Foo()                    ' Foo instance created
    f.Bar =  i * 10
    FooList.Add(f)                   ' Foo object added to list
Next

有关详细信息,请参阅:


列表和集合

.NET 集合(其中有很多种类 - 列表、字典等)也必须被实例化或创建。

Private myList As List(Of String)
..
myList.Add("ziggy")           ' NullReference

由于相同的原因,您会得到相同的异常 - 仅声明了 myList,但未创建实例。补救措施是相同的:

myList = New List(Of String)

' Or create an instance when declared:
Private myList As New List(Of String)

常见的疏忽是使用集合 Type 的类:

Public Class Foo
    Private barList As List(Of Bar)

    Friend Function BarCount As Integer
        Return barList.Count
    End Function

    Friend Sub AddItem(newBar As Bar)
        If barList.Contains(newBar) = False Then
            barList.Add(newBar)
        End If
    End Function

任一过程都会导致 NRE,因为 barList 仅被声明,而非实例化。创建 Foo 的实例不会同时创建内部 barList 的实例。可能是想在构造函数中执行此操作:

Public Sub New         ' Constructor
    ' Stuff to do when a new Foo is created...
    barList = New List(Of Bar)
End Sub

与以前一样,这是不正确的:

Public Sub New()
    ' Creates another barList local to this procedure
     Dim barList As New List(Of Bar)
End Sub

有关详细信息,请参阅 List(Of T)


数据提供者对象

使用数据库为 NullReference 提供了许多机会,因为可以有许多对象(CommandConnectionTransactionDataset DataTableDataRows....) 立即使用。 注意:无论您使用哪个数据提供程序(MySQL、SQL Server、OleDB 等),概念都是相同的。

示例 1

Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim MaxRows As Integer

con.Open()
Dim sql = "SELECT * FROM tblfoobar_List"
da = New OleDbDataAdapter(sql, con)
da.Fill(ds, "foobar")
con.Close()

MaxRows = ds.Tables("foobar").Rows.Count      ' Error

与之前一样,声明了 ds Dataset 对象,但从未创建实例。 DataAdapter 将填充现有的DataSet,而不是创建一个。在这种情况下,由于 ds 是局部变量,因此 IDE 会警告您可能会发生这种情况:

img

当声明为模块/类级别变量时,如 con 的情况,编译器无法知道该对象是否由上游过程创建。不要忽视警告。

补救措施

Dim ds As New DataSet

示例 2

ds = New DataSet
da = New OleDBDataAdapter(sql, con)
da.Fill(ds, "Employees")

txtID.Text = ds.Tables("Employee").Rows(0).Item(1)
txtID.Name = ds.Tables("Employee").Rows(0).Item(2)

这里存在拼写错误的问题:EmployeesEmployee。没有创建名为“Employee”的 DataTable,因此尝试访问它会导致 NullReferenceException 。另一个潜在的问题是假设存在 Items,但当 SQL 包含 WHERE 子句时,情况可能并非如此。

补救措施

由于这使用一个表,因此使用 Tables(0) 将避免拼写错误。检查 Rows.Count 也有帮助:

If ds.Tables(0).Rows.Count > 0 Then
    txtID.Text = ds.Tables(0).Rows(0).Item(1)
    txtID.Name = ds.Tables(0).Rows(0).Item(2)
End If

Fill 是一个返回受影响的 Rows 数量的函数,也可以对其进行测试:

If da.Fill(ds, "Employees") > 0 Then...

示例 3

Dim da As New OleDb.OleDbDataAdapter("SELECT TICKET.TICKET_NO,
        TICKET.CUSTOMER_ID, ... FROM TICKET_RESERVATION AS TICKET INNER JOIN
        FLIGHT_DETAILS AS FLIGHT ... WHERE [TICKET.TICKET_NO]= ...", con)
Dim ds As New DataSet
da.Fill(ds)

If ds.Tables("TICKET_RESERVATION").Rows.Count > 0 Then

DataAdapter 将提供 TableNames,如上例所示,但它不会解析 SQL 或数据库表中的名称。因此,ds.Tables("TICKET_RESERVATION") 引用了一个不存在的表。

补救措施是相同的,通过索引引用表格:

If ds.Tables(0).Rows.Count > 0 Then

另请参阅DataTable 类


对象路径/嵌套

If myFoo.Bar.Items IsNot Nothing Then
   ...

代码仅测试 Items,而 myFooBar 也可能是 Nothing。 补救措施是一次测试一个对象的整个链或路径:

If (myFoo IsNot Nothing) AndAlso
    (myFoo.Bar IsNot Nothing) AndAlso
    (myFoo.Bar.Items IsNot Nothing) Then
    ....

AndAlso 很重要。一旦遇到第一个 False 条件,后续测试将不会执行。这允许代码一次安全地“钻取”对象一个“级别”,仅在确定 myFoo 后(并且如果)才评估 myFoo.Bar是有效的。在对复杂对象进行编码时,对象链或路径可能会变得相当长:

myBase.myNodes(3).Layer.SubLayer.Foo.Files.Add("somefilename")

不可能引用 null 对象的任何“下游”内容。这也适用于控件:

myWebBrowser.Document.GetElementById("formfld1").InnerText = "some value"

这里,myWebBrowserDocument 可以为 Nothing,或者 formfld1 元素可能不存在。


UI 控件

Dim cmd5 As New SqlCommand("select Cartons, Pieces, Foobar " _
     & "FROM Invoice where invoice_no = '" & _
     Me.ComboBox5.SelectedItem.ToString.Trim & "' And category = '" & _
     Me.ListBox1.SelectedItem.ToString.Trim & "' And item_name = '" & _
     Me.ComboBox2.SelectedValue.ToString.Trim & "' And expiry_date = '" & _
     Me.expiry.Text & "'", con)

除此之外,此代码并未预期用户可能未在一个或多个 UI 控件中选择某些内容。 ListBox1.SelectedItem 很可能是 Nothing,因此 ListBox1.SelectedItem.ToString 将导致 NRE。

补救措施

在使用数据之前验证数据(也使用Option Strict和SQL参数):

Dim expiry As DateTime         ' for text date validation
If (ComboBox5.SelectedItems.Count > 0) AndAlso
    (ListBox1.SelectedItems.Count > 0) AndAlso
    (ComboBox2.SelectedItems.Count > 0) AndAlso
    (DateTime.TryParse(expiry.Text, expiry) Then

    '... do stuff
Else
    MessageBox.Show(...error message...)
End If

或者,您可以使用(ComboBox5.SelectedItem IsNot Nothing) AndAlso...< /code>


Visual Basic Forms

Public Class Form1

    Private NameBoxes = New TextBox(5) {Controls("TextBox1"), _
                   Controls("TextBox2"), Controls("TextBox3"), _
                   Controls("TextBox4"), Controls("TextBox5"), _
                   Controls("TextBox6")}

    ' same thing in a different format:
    Private boxList As New List(Of TextBox) From {TextBox1, TextBox2, TextBox3 ...}

    ' Immediate NRE:
    Private somevar As String = Me.Controls("TextBox1").Text

这是获取 NRE 的相当常见的方法。在 C# 中,根据编码方式,IDE 将报告当前上下文中不存在 Controls,或“无法引用非静态成员”。所以,从某种程度上来说,这是一种仅限 VB 的情况。它也很复杂,因为它可能导致级联故障。

数组和集合不能以这种方式初始化。此初始化代码将在构造函数创建 FormControls.结果:

  • 列表和集合将只是空
  • 数组将包含 Nothing 的五个元素
  • somevar 赋值将导致立即 NRE,因为 Nothing 没有 .Text property

稍后引用数组元素将产生 NRE。如果您在 Form_Load 中执行此操作,由于奇怪的错误,IDE 可能不会在发生异常时报告该异常。当您的代码尝试使用该数组时,稍后会弹出异常。 这篇文章详细介绍了这种“静默异常”。就我们的目的而言,关键是当创建表单时发生灾难性事件(Sub NewForm Load 事件)时,可能不会报告异常,代码会退出过程并仅显示表格。

由于 Sub NewForm Load 事件中的其他代码不会在 NRE 之后运行,因此许多其他内容可以保持未初始化。

Sub Form_Load(..._
   '...
   Dim name As String = NameBoxes(2).Text        ' NRE
   ' ...
   ' More code (which will likely not be executed)
   ' ...
End Sub

注意这适用于任何和所有控件和组件引用,使得这些引用在它们所在的地方都是非法的:

Public Class Form1

    Private myFiles() As String = Me.OpenFileDialog1.FileName & ...
    Private dbcon As String = OpenFileDialog1.FileName & ";Jet Oledb..."
    Private studentName As String = TextBox13.Text

部分补救措施

奇怪的是 VB 没有提供警告,但补救措施是在表单级别声明容器,但当控件确实存在时,在表单加载事件处理程序中初始化它们。只要您的代码位于 InitializeComponent 调用之后,就可以在 Sub New 中完成此操作:

' Module level declaration
Private NameBoxes as TextBox()
Private studentName As String

' Form Load, Form Shown or Sub New:
'
' Using the OP's approach (illegal using OPTION STRICT)
NameBoxes = New TextBox() {Me.Controls("TextBox1"), Me.Controls("TestBox2"), ...)
studentName = TextBox32.Text           ' For simple control references

数组代码可能尚未脱离困境。容器控件(例如 GroupBoxPanel)中的任何控件都不会在 Me.Controls 中找到;它们将位于该面板或组框的控件集合中。当控件名称拼写错误时 ("TeStBox2"),也不会返回控件。在这种情况下,Nothing 将再次存储在这些数组元素中,并且当您尝试引用它时将产生 NRE。

既然您知道自己在寻找什么,那么这些应该很容易找到:
VS 显示您方法的错误

“Button2”位于面板

补救措施

不要使用表单的 Controls 集合按名称间接引用,而是使用控件引用:

' Declaration
Private NameBoxes As TextBox()

' Initialization -  simple and easy to read, hard to botch:
NameBoxes = New TextBox() {TextBox1, TextBox2, ...)

' Initialize a List
NamesList = New List(Of TextBox)({TextBox1, TextBox2, TextBox3...})
' or
NamesList = New List(Of TextBox)
NamesList.AddRange({TextBox1, TextBox2, TextBox3...})

函数不返回任何内容

Private bars As New List(Of Bars)        ' Declared and created

Public Function BarList() As List(Of Bars)
    bars.Clear
    If someCondition Then
        For n As Integer = 0 to someValue
            bars.Add(GetBar(n))
        Next n
    Else
        Exit Function
    End If

    Return bars
End Function

在这种情况下,IDE 会警告您“并非所有路径都返回”一个值和一个NullReferenceException 可能会导致'。您可以通过将 Exit Function 替换为 Return Nothing 来抑制警告,但这并不能解决问题。当 someCondition = False 时尝试使用 return 的任何操作都会导致 NRE:

bList = myFoo.BarList()
For Each b As Bar in bList      ' EXCEPTION
      ...

补救措施

将函数中的 Exit Function 替换为 Return b列表。返回 List 与返回Nothing 不同。如果返回的对象有可能Nothing,请在使用它之前进行测试:

 bList = myFoo.BarList()
 If bList IsNot Nothing Then...

实施不佳的 Try/Catch

实施不当的 Try/Catch 可能会隐藏问题所在并导致新的问题:

Dim dr As SqlDataReader
Try
    Dim lnk As LinkButton = TryCast(sender, LinkButton)
    Dim gr As GridViewRow = DirectCast(lnk.NamingContainer, GridViewRow)
    Dim eid As String = GridView1.DataKeys(gr.RowIndex).Value.ToString()
    ViewState("username") = eid
    sqlQry = "select FirstName, Surname, DepartmentName, ExtensionName, jobTitle,
             Pager, mailaddress, from employees1 where username='" & eid & "'"
    If connection.State <> ConnectionState.Open Then
        connection.Open()
    End If
    command = New SqlCommand(sqlQry, connection)

    'More code fooing and barring

    dr = command.ExecuteReader()
    If dr.Read() Then
        lblFirstName.Text = Convert.ToString(dr("FirstName"))
        ...
    End If
    mpe.Show()
Catch

Finally
    command.Dispose()
    dr.Close()             ' <-- NRE
    connection.Close()
End Try

这是未按预期创建对象的情况,但也演示了空 Catch 的计数器用途。

SQL 中有一个额外的逗号(在“mailaddress”之后),这会导致 .ExecuteReader 出现异常。在 Catch 不执行任何操作后,Finally 尝试执行清理,但由于您无法 CloseDataReader 对象,一个全新的 NullReferenceException 结果。

空的 Catch 块是魔鬼的游乐场。这位 OP 很困惑为什么他会在 Finally 块中得到一个 NRE。在其他情况下,空的 Catch 可能会导致下游的其他事情变得混乱,并导致您花时间在错误的位置查找错误的内容来解决问题。 (上述“静默异常”提供了相同的娱乐价值。)

补救措施

不要使用空的 Try/Catch 块 - 让代码崩溃,以便您可以 a) 确定原因 b) 确定位置c) 采取适当的补救措施。 Try/Catch 块并不是为了向唯一有资格修复它们的人(即开发人员)隐藏异常。


DBNull 与 Nothing 不同

For Each row As DataGridViewRow In dgvPlanning.Rows
    If Not IsDBNull(row.Cells(0).Value) Then
        ...

IsDBNull 函数用于测试是否等于 System.DBNull来自MSDN:

System.DBNull 值指示该对象表示丢失或不存在的数据。 DBNull 与 Nothing 不同,Nothing 表示变量尚未初始化。

补救措施

If row.Cells(0) IsNot Nothing Then ...

与之前一样,您可以测试 Nothing,然后测试特定值:

If (row.Cells(0) IsNot Nothing) AndAlso (IsDBNull(row.Cells(0).Value) = False) Then

示例 2

Dim getFoo = (From f In dbContext.FooBars
               Where f.something = something
               Select f).FirstOrDefault

If Not IsDBNull(getFoo) Then
    If IsDBNull(getFoo.user_id) Then
        txtFirst.Text = getFoo.first_name
    Else
       ...

FirstOrDefault 返回第一项或默认值,其中对于引用类型来说是 Nothing,并且从不 DBNull

If getFoo IsNot Nothing Then...

控制

Dim chk As CheckBox

chk = CType(Me.Controls(chkName), CheckBox)
If chk.Checked Then
    Return chk
End If

如果无法找到带有 chkNameCheckBox(或者存在于一个GroupBox),那么 chk 将为 Nothing,并且尝试引用任何属性都会导致异常。

补救措施

If (chk IsNot Nothing) AndAlso (chk.Checked) Then ...

DataGridView

DGV 有一些定期出现的怪癖:

dgvBooks.DataSource = loan.Books
dgvBooks.Columns("ISBN").Visible = True       ' NullReferenceException
dgvBooks.Columns("Title").DefaultCellStyle.Format = "C"
dgvBooks.Columns("Author").DefaultCellStyle.Format = "C"
dgvBooks.Columns("Price").DefaultCellStyle.Format = "C"

如果 dgvBooks 具有 AutoGenerateColumns = True,它将创建列,但不会命名它们,因此上面的代码在按名称引用它们时会失败。

补救措施

手动命名列,或按索引引用:

dgvBooks.Columns(0).Visible = True

请注意 NewRow

xlWorkSheet = xlWorkBook.Sheets("sheet1")

For i = 0 To myDGV.RowCount - 1
    For j = 0 To myDGV.ColumnCount - 1
        For k As Integer = 1 To myDGV.Columns.Count
            xlWorkSheet.Cells(1, k) = myDGV.Columns(k - 1).HeaderText
            xlWorkSheet.Cells(i + 2, j + 1) = myDGV(j, i).Value.ToString()
        Next
    Next
Next

示例 2 -当您的 DataGridViewAllowUserToAddRows 设置为 True< 时, /code> (默认),底部空白/新行中的 Cells 将全部包含 Nothing。大多数使用内容的尝试(例如,ToString)都会导致 NRE。

补救措施

使用 For/Each 循环并测试 IsNewRow 属性以确定它是否是最后一行。无论 AllowUserToAddRows 是否为真,这都有效:

For Each r As DataGridViewRow in myDGV.Rows
    If r.IsNewRow = False Then
         ' ok to use this row

如果您确实使用 For n 循环,请修改行数或在 Exit For 时使用>IsNewRow 为 true。


My.Settings (StringCollection)

在某些情况下,尝试使用 My.Settings 中的一个 StringCollection 项目可能会在您第一次使用它时导致 NullReference。解决方案是相同的,但不那么明显。考虑一下:

My.Settings.FooBars.Add("ziggy")         ' foobars is a string collection

由于 VB 正在为您管理“设置”,因此期望它初始化该集合是合理的。它会,但前提是您之前已将初始条目添加到集合中(在“设置”编辑器中)。由于该集合(显然)是在添加项目时初始化的,因此当“设置”编辑器中没有要添加的项目时,该集合将保持 Nothing 状态。

补救措施

如果需要的话,在表单的 Load 事件处理程序中初始化设置集合:

If My.Settings.FooBars Is Nothing Then
    My.Settings.FooBars = New System.Collections.Specialized.StringCollection
End If

通常,Settings 集合只需要在以下情况下初始化:应用程序第一次运行时。另一种补救措施是在项目 -> 中为您的集合添加初始值。设置 | FooBars,保存项目,然后删除假值。


要点

您可能忘记了 New 运算符。

或者

您认为可以完美地执行以将初始化的对象返回到您的代码的某些操作,但事实并非如此。

不要忽略编译器警告(永远)并使用Option Strict On(始终)。


MSDN NullReference 异常

NullReference Exception — Visual Basic

The NullReference Exception for Visual Basic is no different from the one in C#. After all, they are both reporting the same exception defined in the .NET Framework which they both use. Causes unique to Visual Basic are rare (perhaps only one).

This answer will use Visual Basic terms, syntax, and context. The examples used come from a large number of past Stack  Overflow questions. This is to maximize relevance by using the kinds of situations often seen in posts. A bit more explanation is also provided for those who might need it. An example similar to yours is very likely listed here.

Note:

  1. This is concept-based: there is no code for you to paste into your project. It is intended to help you understand what causes a NullReferenceException (NRE), how to find it, how to fix it, and how to avoid it. An NRE can be caused many ways so this is unlikely to be your sole encounter.
  2. The examples (from Stack  Overflow posts) do not always show the best way to do something in the first place.
  3. Typically, the simplest remedy is used.

Basic Meaning

The message "Object not set to an instance of Object" means you are trying to use an object which has not been initialized. This boils down to one of these:

  • Your code declared an object variable, but it did not initialize it (create an instance or 'instantiate' it)
  • Something which your code assumed would initialize an object, did not
  • Possibly, other code prematurely invalidated an object still in use

Finding The Cause

Since the problem is an object reference which is Nothing, the answer is to examine them to find out which one. Then determine why it is not initialized. Hold the mouse over the various variables and Visual Studio (VS) will show their values - the culprit will be Nothing.

IDE debug display

You should also remove any Try/Catch blocks from the relevant code, especially ones where there is nothing in the Catch block. This will cause your code to crash when it tries to use an object which is Nothing. This is what you want because it will identify the exact location of the problem, and allow you to identify the object causing it.

A MsgBox in the Catch which displays Error while... will be of little help. This method also leads to very bad Stack  Overflow questions, because you can't describe the actual exception, the object involved or even the line of code where it happens.

You can also use the Locals Window (Debug -> Windows -> Locals) to examine your objects.

Once you know what and where the problem is, it is usually fairly easy to fix and faster than posting a new question.

See also:

Examples and Remedies

Class Objects / Creating an Instance

Dim reg As CashRegister
...
TextBox1.Text = reg.Amount         ' NRE

The problem is that Dim does not create a CashRegister object; it only declares a variable named reg of that Type. Declaring an object variable and creating an instance are two different things.

Remedy

The New operator can often be used to create the instance when you declare it:

Dim reg As New CashRegister        ' [New] creates instance, invokes the constructor

' Longer, more explicit form:
Dim reg As CashRegister = New CashRegister

When it is only appropriate to create the instance later:

Private reg As CashRegister         ' Declare
  ...
reg = New CashRegister()            ' Create instance

Note: Do not use Dim again in a procedure, including the constructor (Sub New):

Private reg As CashRegister
'...

Public Sub New()
   '...
   Dim reg As New CashRegister
End Sub

This will create a local variable, reg, which exists only in that context (sub). The reg variable with module level Scope which you will use everywhere else remains Nothing.

Missing the New operator is the #1 cause of NullReference Exceptions seen in the Stack  Overflow questions reviewed.

Visual Basic tries to make the process clear repeatedly using New: Using the New Operator creates a new object and calls Sub New -- the constructor -- where your object can perform any other initialization.

To be clear, Dim (or Private) only declares a variable and its Type. The Scope of the variable - whether it exists for the entire module/class or is local to a procedure - is determined by where it is declared. Private | Friend | Public defines the access level, not Scope.

For more information, see:


Arrays

Arrays must also be instantiated:

Private arr as String()

This array has only been declared, not created. There are several ways to initialize an array:

Private arr as String() = New String(10){}
' or
Private arr() As String = New String(10){}

' For a local array (in a procedure) and using 'Option Infer':
Dim arr = New String(10) {}

Note: Beginning with VS 2010, when initializing a local array using a literal and Option Infer, the As <Type> and New elements are optional:

Dim myDbl As Double() = {1.5, 2, 9.9, 18, 3.14}
Dim myDbl = New Double() {1.5, 2, 9.9, 18, 3.14}
Dim myDbl() = {1.5, 2, 9.9, 18, 3.14}

The data Type and array size are inferred from the data being assigned. Class/Module level declarations still require As <Type> with Option Strict:

Private myDoubles As Double() = {1.5, 2, 9.9, 18, 3.14}

Example: Array of class objects

Dim arrFoo(5) As Foo

For i As Integer = 0 To arrFoo.Count - 1
   arrFoo(i).Bar = i * 10       ' Exception
Next

The array has been created, but the Foo objects in it have not.

Remedy

For i As Integer = 0 To arrFoo.Count - 1
    arrFoo(i) = New Foo()         ' Create Foo instance
    arrFoo(i).Bar = i * 10
Next

Using a List(Of T) will make it quite difficult to have an element without a valid object:

Dim FooList As New List(Of Foo)     ' List created, but it is empty
Dim f As Foo                        ' Temporary variable for the loop

For i As Integer = 0 To 5
    f = New Foo()                    ' Foo instance created
    f.Bar =  i * 10
    FooList.Add(f)                   ' Foo object added to list
Next

For more information, see:


Lists and Collections

.NET collections (of which there are many varieties - Lists, Dictionary, etc.) must also be instantiated or created.

Private myList As List(Of String)
..
myList.Add("ziggy")           ' NullReference

You get the same exception for the same reason - myList was only declared, but no instance created. The remedy is the same:

myList = New List(Of String)

' Or create an instance when declared:
Private myList As New List(Of String)

A common oversight is a class which uses a collection Type:

Public Class Foo
    Private barList As List(Of Bar)

    Friend Function BarCount As Integer
        Return barList.Count
    End Function

    Friend Sub AddItem(newBar As Bar)
        If barList.Contains(newBar) = False Then
            barList.Add(newBar)
        End If
    End Function

Either procedure will result in an NRE, because barList is only declared, not instantiated. Creating an instance of Foo will not also create an instance of the internal barList. It may have been the intent to do this in the constructor:

Public Sub New         ' Constructor
    ' Stuff to do when a new Foo is created...
    barList = New List(Of Bar)
End Sub

As before, this is incorrect:

Public Sub New()
    ' Creates another barList local to this procedure
     Dim barList As New List(Of Bar)
End Sub

For more information, see List(Of T) Class.


Data Provider Objects

Working with databases presents many opportunities for a NullReference because there can be many objects (Command, Connection, Transaction, Dataset, DataTable, DataRows....) in use at once. Note: It does not matter which data provider you are using -- MySQL, SQL Server, OleDB, etc. -- the concepts are the same.

Example 1

Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim MaxRows As Integer

con.Open()
Dim sql = "SELECT * FROM tblfoobar_List"
da = New OleDbDataAdapter(sql, con)
da.Fill(ds, "foobar")
con.Close()

MaxRows = ds.Tables("foobar").Rows.Count      ' Error

As before, the ds Dataset object was declared, but an instance was never created. The DataAdapter will fill an existing DataSet, not create one. In this case, since ds is a local variable, the IDE warns you that this might happen:

img

When declared as a module/class level variable, as appears to be the case with con, the compiler can't know if the object was created by an upstream procedure. Do not ignore warnings.

Remedy

Dim ds As New DataSet

Example 2

ds = New DataSet
da = New OleDBDataAdapter(sql, con)
da.Fill(ds, "Employees")

txtID.Text = ds.Tables("Employee").Rows(0).Item(1)
txtID.Name = ds.Tables("Employee").Rows(0).Item(2)

A typo is a problem here: Employees vs Employee. There was no DataTable named "Employee" created, so a NullReferenceException results trying to access it. Another potential problem is assuming there will be Items which may not be so when the SQL includes a WHERE clause.

Remedy

Since this uses one table, using Tables(0) will avoid spelling errors. Examining Rows.Count can also help:

If ds.Tables(0).Rows.Count > 0 Then
    txtID.Text = ds.Tables(0).Rows(0).Item(1)
    txtID.Name = ds.Tables(0).Rows(0).Item(2)
End If

Fill is a function returning the number of Rows affected which can also be tested:

If da.Fill(ds, "Employees") > 0 Then...

Example 3

Dim da As New OleDb.OleDbDataAdapter("SELECT TICKET.TICKET_NO,
        TICKET.CUSTOMER_ID, ... FROM TICKET_RESERVATION AS TICKET INNER JOIN
        FLIGHT_DETAILS AS FLIGHT ... WHERE [TICKET.TICKET_NO]= ...", con)
Dim ds As New DataSet
da.Fill(ds)

If ds.Tables("TICKET_RESERVATION").Rows.Count > 0 Then

The DataAdapter will provide TableNames as shown in the previous example, but it does not parse names from the SQL or database table. As a result, ds.Tables("TICKET_RESERVATION") references a non-existent table.

The Remedy is the same, reference the table by index:

If ds.Tables(0).Rows.Count > 0 Then

See also DataTable Class.


Object Paths / Nested

If myFoo.Bar.Items IsNot Nothing Then
   ...

The code is only testing Items while both myFoo and Bar may also be Nothing. The remedy is to test the entire chain or path of objects one at a time:

If (myFoo IsNot Nothing) AndAlso
    (myFoo.Bar IsNot Nothing) AndAlso
    (myFoo.Bar.Items IsNot Nothing) Then
    ....

AndAlso is important. Subsequent tests will not be performed once the first False condition is encountered. This allows the code to safely 'drill' into the object(s) one 'level' at a time, evaluating myFoo.Bar only after (and if) myFoo is determined to be valid. Object chains or paths can get quite long when coding complex objects:

myBase.myNodes(3).Layer.SubLayer.Foo.Files.Add("somefilename")

It is not possible to reference anything 'downstream' of a null object. This also applies to controls:

myWebBrowser.Document.GetElementById("formfld1").InnerText = "some value"

Here, myWebBrowser or Document could be Nothing or the formfld1 element may not exist.


UI Controls

Dim cmd5 As New SqlCommand("select Cartons, Pieces, Foobar " _
     & "FROM Invoice where invoice_no = '" & _
     Me.ComboBox5.SelectedItem.ToString.Trim & "' And category = '" & _
     Me.ListBox1.SelectedItem.ToString.Trim & "' And item_name = '" & _
     Me.ComboBox2.SelectedValue.ToString.Trim & "' And expiry_date = '" & _
     Me.expiry.Text & "'", con)

Among other things, this code does not anticipate that the user may not have selected something in one or more UI controls. ListBox1.SelectedItem may well be Nothing, so ListBox1.SelectedItem.ToString will result in an NRE.

Remedy

Validate data before using it (also use Option Strict and SQL parameters):

Dim expiry As DateTime         ' for text date validation
If (ComboBox5.SelectedItems.Count > 0) AndAlso
    (ListBox1.SelectedItems.Count > 0) AndAlso
    (ComboBox2.SelectedItems.Count > 0) AndAlso
    (DateTime.TryParse(expiry.Text, expiry) Then

    '... do stuff
Else
    MessageBox.Show(...error message...)
End If

Alternatively, you can use (ComboBox5.SelectedItem IsNot Nothing) AndAlso...


Visual Basic Forms

Public Class Form1

    Private NameBoxes = New TextBox(5) {Controls("TextBox1"), _
                   Controls("TextBox2"), Controls("TextBox3"), _
                   Controls("TextBox4"), Controls("TextBox5"), _
                   Controls("TextBox6")}

    ' same thing in a different format:
    Private boxList As New List(Of TextBox) From {TextBox1, TextBox2, TextBox3 ...}

    ' Immediate NRE:
    Private somevar As String = Me.Controls("TextBox1").Text

This is a fairly common way to get an NRE. In C#, depending on how it is coded, the IDE will report that Controls does not exist in the current context, or "cannot reference non-static member". So, to some extent, this is a VB-only situation. It is also complex because it can result in a failure cascade.

The arrays and collections cannot be initialized this way. This initialization code will run before the constructor creates the Form or the Controls. As a result:

  • Lists and Collection will simply be empty
  • The Array will contain five elements of Nothing
  • The somevar assignment will result in an immediate NRE because Nothing doesn't have a .Text property

Referencing array elements later will result in an NRE. If you do this in Form_Load, due to an odd bug, the IDE may not report the exception when it happens. The exception will pop up later when your code tries to use the array. This "silent exception" is detailed in this post. For our purposes, the key is that when something catastrophic happens while creating a form (Sub New or Form Load event), exceptions may go unreported, the code exits the procedure and just displays the form.

Since no other code in your Sub New or Form Load event will run after the NRE, a great many other things can be left uninitialized.

Sub Form_Load(..._
   '...
   Dim name As String = NameBoxes(2).Text        ' NRE
   ' ...
   ' More code (which will likely not be executed)
   ' ...
End Sub

Note this applies to any and all control and component references making these illegal where they are:

Public Class Form1

    Private myFiles() As String = Me.OpenFileDialog1.FileName & ...
    Private dbcon As String = OpenFileDialog1.FileName & ";Jet Oledb..."
    Private studentName As String = TextBox13.Text

Partial Remedy

It is curious that VB does not provide a warning, but the remedy is to declare the containers at the form level, but initialize them in form load event handler when the controls do exist. This can be done in Sub New as long as your code is after the InitializeComponent call:

' Module level declaration
Private NameBoxes as TextBox()
Private studentName As String

' Form Load, Form Shown or Sub New:
'
' Using the OP's approach (illegal using OPTION STRICT)
NameBoxes = New TextBox() {Me.Controls("TextBox1"), Me.Controls("TestBox2"), ...)
studentName = TextBox32.Text           ' For simple control references

The array code may not be out of the woods yet. Any controls which are in a container control (like a GroupBox or Panel) will not be found in Me.Controls; they will be in the Controls collection of that Panel or GroupBox. Nor will a control be returned when the control name is misspelled ("TeStBox2"). In such cases, Nothing will again be stored in those array elements and an NRE will result when you attempt to reference it.

These should be easy to find now that you know what you are looking for:
VS shows you the error of your ways

"Button2" resides on a Panel

Remedy

Rather than indirect references by name using the form's Controls collection, use the control reference:

' Declaration
Private NameBoxes As TextBox()

' Initialization -  simple and easy to read, hard to botch:
NameBoxes = New TextBox() {TextBox1, TextBox2, ...)

' Initialize a List
NamesList = New List(Of TextBox)({TextBox1, TextBox2, TextBox3...})
' or
NamesList = New List(Of TextBox)
NamesList.AddRange({TextBox1, TextBox2, TextBox3...})

Function Returning Nothing

Private bars As New List(Of Bars)        ' Declared and created

Public Function BarList() As List(Of Bars)
    bars.Clear
    If someCondition Then
        For n As Integer = 0 to someValue
            bars.Add(GetBar(n))
        Next n
    Else
        Exit Function
    End If

    Return bars
End Function

This is a case where the IDE will warn you that 'not all paths return a value and a NullReferenceException may result'. You can suppress the warning, by replacing Exit Function with Return Nothing, but that does not solve the problem. Anything which tries to use the return when someCondition = False will result in an NRE:

bList = myFoo.BarList()
For Each b As Bar in bList      ' EXCEPTION
      ...

Remedy

Replace Exit Function in the function with Return bList. Returning an empty List is not the same as returning Nothing. If there is a chance that a returned object can be Nothing, test before using it:

 bList = myFoo.BarList()
 If bList IsNot Nothing Then...

Poorly Implemented Try/Catch

A badly implemented Try/Catch can hide where the problem is and result in new ones:

Dim dr As SqlDataReader
Try
    Dim lnk As LinkButton = TryCast(sender, LinkButton)
    Dim gr As GridViewRow = DirectCast(lnk.NamingContainer, GridViewRow)
    Dim eid As String = GridView1.DataKeys(gr.RowIndex).Value.ToString()
    ViewState("username") = eid
    sqlQry = "select FirstName, Surname, DepartmentName, ExtensionName, jobTitle,
             Pager, mailaddress, from employees1 where username='" & eid & "'"
    If connection.State <> ConnectionState.Open Then
        connection.Open()
    End If
    command = New SqlCommand(sqlQry, connection)

    'More code fooing and barring

    dr = command.ExecuteReader()
    If dr.Read() Then
        lblFirstName.Text = Convert.ToString(dr("FirstName"))
        ...
    End If
    mpe.Show()
Catch

Finally
    command.Dispose()
    dr.Close()             ' <-- NRE
    connection.Close()
End Try

This is a case of an object not being created as expected, but also demonstrates the counter usefulness of an empty Catch.

There is an extra comma in the SQL (after 'mailaddress') which results in an exception at .ExecuteReader. After the Catch does nothing, Finally tries to perform clean up, but since you cannot Close a null DataReader object, a brand new NullReferenceException results.

An empty Catch block is the devil's playground. This OP was baffled why he was getting an NRE in the Finally block. In other situations, an empty Catch may result in something else much further downstream going haywire and cause you to spend time looking at the wrong things in the wrong place for the problem. (The "silent exception" described above provides the same entertainment value.)

Remedy

Don't use empty Try/Catch blocks - let the code crash so you can a) identify the cause b) identify the location and c) apply a proper remedy. Try/Catch blocks are not intended to hide exceptions from the person uniquely qualified to fix them - the developer.


DBNull is not the same as Nothing

For Each row As DataGridViewRow In dgvPlanning.Rows
    If Not IsDBNull(row.Cells(0).Value) Then
        ...

The IsDBNull function is used to test if a value equals System.DBNull: From MSDN:

The System.DBNull value indicates that the Object represents missing or non-existent data. DBNull is not the same as Nothing, which indicates that a variable has not yet been initialized.

Remedy

If row.Cells(0) IsNot Nothing Then ...

As before, you can test for Nothing, then for a specific value:

If (row.Cells(0) IsNot Nothing) AndAlso (IsDBNull(row.Cells(0).Value) = False) Then

Example 2

Dim getFoo = (From f In dbContext.FooBars
               Where f.something = something
               Select f).FirstOrDefault

If Not IsDBNull(getFoo) Then
    If IsDBNull(getFoo.user_id) Then
        txtFirst.Text = getFoo.first_name
    Else
       ...

FirstOrDefault returns the first item or the default value, which is Nothing for reference types and never DBNull:

If getFoo IsNot Nothing Then...

Controls

Dim chk As CheckBox

chk = CType(Me.Controls(chkName), CheckBox)
If chk.Checked Then
    Return chk
End If

If a CheckBox with chkName can't be found (or exists in a GroupBox), then chk will be Nothing and be attempting to reference any property will result in an exception.

Remedy

If (chk IsNot Nothing) AndAlso (chk.Checked) Then ...

The DataGridView

The DGV has a few quirks seen periodically:

dgvBooks.DataSource = loan.Books
dgvBooks.Columns("ISBN").Visible = True       ' NullReferenceException
dgvBooks.Columns("Title").DefaultCellStyle.Format = "C"
dgvBooks.Columns("Author").DefaultCellStyle.Format = "C"
dgvBooks.Columns("Price").DefaultCellStyle.Format = "C"

If dgvBooks has AutoGenerateColumns = True, it will create the columns, but it does not name them, so the above code fails when it references them by name.

Remedy

Name the columns manually, or reference by index:

dgvBooks.Columns(0).Visible = True

Example 2 — Beware of the NewRow

xlWorkSheet = xlWorkBook.Sheets("sheet1")

For i = 0 To myDGV.RowCount - 1
    For j = 0 To myDGV.ColumnCount - 1
        For k As Integer = 1 To myDGV.Columns.Count
            xlWorkSheet.Cells(1, k) = myDGV.Columns(k - 1).HeaderText
            xlWorkSheet.Cells(i + 2, j + 1) = myDGV(j, i).Value.ToString()
        Next
    Next
Next

When your DataGridView has AllowUserToAddRows as True (the default), the Cells in the blank/new row at the bottom will all contain Nothing. Most attempts to use the contents (for example, ToString) will result in an NRE.

Remedy

Use a For/Each loop and test the IsNewRow property to determine if it is that last row. This works whether AllowUserToAddRows is true or not:

For Each r As DataGridViewRow in myDGV.Rows
    If r.IsNewRow = False Then
         ' ok to use this row

If you do use a For n loop, modify the row count or use Exit For when IsNewRow is true.


My.Settings (StringCollection)

Under certain circumstances, trying to use an item from My.Settings which is a StringCollection can result in a NullReference the first time you use it. The solution is the same, but not as obvious. Consider:

My.Settings.FooBars.Add("ziggy")         ' foobars is a string collection

Since VB is managing Settings for you, it is reasonable to expect it to initialize the collection. It will, but only if you have previously added an initial entry to the collection (in the Settings editor). Since the collection is (apparently) initialized when an item is added, it remains Nothing when there are no items in the Settings editor to add.

Remedy

Initialize the settings collection in the form's Load event handler, if/when needed:

If My.Settings.FooBars Is Nothing Then
    My.Settings.FooBars = New System.Collections.Specialized.StringCollection
End If

Typically, the Settings collection will only need to be initialized the first time the application runs. An alternate remedy is to add an initial value to your collection in Project -> Settings | FooBars, save the project, then remove the fake value.


Key Points

You probably forgot the New operator.

or

Something you assumed would perform flawlessly to return an initialized object to your code, did not.

Don't ignore compiler warnings (ever) and use Option Strict On (always).


MSDN NullReference Exception

昇り龍 2025-01-24 18:31:01

另一种情况是,当您将null对象施加到a 。例如,下面的代码:

object o = null;
DateTime d = (DateTime)o;

它将在铸件上抛出 nullReferenceException 。在上面的示例中似乎很明显,但是这可以在更多的“后期”错综复杂的方案中发生,在这些方案中,从您不拥有的某些代码返回了空对象,并且演员表是由某些自动系统生成的。

一个示例是使用日历控件的简单ASP.NET绑定片段:

<asp:Calendar runat="server" SelectedDate="<%#Bind("Something")%>" />

selected -Date 实际上是 dateTime type -of calendar的属性 - Web控制类型,绑定可以完美返回一些空物。隐式ASP.NET生成器将创建一个代码,该代码等效于上面的铸造代码。这将引起很难发现的 nullReferenceException ,因为它位于ASP.NET生成的代码中,该代码可以编译好...

Another scenario is when you cast a null object into a value type. For example, the code below:

object o = null;
DateTime d = (DateTime)o;

It will throw a NullReferenceException on the cast. It seems quite obvious in the above sample, but this can happen in more "late-binding" intricate scenarios where the null object has been returned from some code you don't own, and the cast is for example generated by some automatic system.

One example of this is this simple ASP.NET binding fragment with the Calendar control:

<asp:Calendar runat="server" SelectedDate="<%#Bind("Something")%>" />

Here, SelectedDate is in fact a property - of DateTime type - of the Calendar Web Control type, and the binding could perfectly return something null. The implicit ASP.NET Generator will create a piece of code that will be equivalent to the cast code above. And this will raise a NullReferenceException that is quite difficult to spot, because it lies in ASP.NET generated code which compiles fine...

﹏雨一样淡蓝的深情 2025-01-24 18:31:01

这意味着您的代码使用了一个设置为 null 的对象引用变量(即它没有引用实际的对象实例)。

为了防止该错误,可能为 null 的对象应在使用之前测试是否为 null。

if (myvar != null)
{
    // Go ahead and use myvar
    myvar.property = ...
}
else
{
    // Whoops! myvar is null and cannot be used without first
    // assigning it to an instance reference
    // Attempting to use myvar here will result in NullReferenceException
}

It means your code used an object reference variable that was set to null (i.e. it did not reference an actual object instance).

To prevent the error, objects that could be null should be tested for null before being used.

if (myvar != null)
{
    // Go ahead and use myvar
    myvar.property = ...
}
else
{
    // Whoops! myvar is null and cannot be used without first
    // assigning it to an instance reference
    // Attempting to use myvar here will result in NullReferenceException
}
命比纸薄 2025-01-24 18:31:01

这意味着所涉及的变量一无所有。我可以这样生成这样的生成:

SqlConnection connection = null;
connection.Open();

这会丢弃错误,因为当我声明变量“ connection ”时,它没有指向任何内容。当我尝试调用成员“ Open ”时,没有参考可以解决,它将引发错误。

为了避免此错误:

  1. 在尝试对其进行任何操作之前,请务必初始化对象。
  2. 如果您不确定对象是否为null,请使用 Object == NULL 检查它。

jetbrains'“ noreferrer”> resharper 工具将确定代码中的每个位置错误,允许您进行零检查。这个错误是恕我直言的错误的第一源。

It means that the variable in question is pointed at nothing. I could generate this like so:

SqlConnection connection = null;
connection.Open();

That will throw the error because while I've declared the variable "connection", it's not pointed to anything. When I try to call the member "Open", there's no reference for it to resolve, and it will throw the error.

To avoid this error:

  1. Always initialize your objects before you try to do anything with them.
  2. If you're not sure whether the object is null, check it with object == null.

JetBrains' ReSharper tool will identify every place in your code that has the possibility of a null reference error, allowing you to put in a null check. This error is the number one source of bugs, IMHO.

如何视而不见 2025-01-24 18:31:01

请注意,无论何种情况,.NET 中的原因始终相同:

您正在尝试使用值为 Nothing/null 的引用变量。当引用变量的值为 Nothing/null 时,这意味着它实际上并未持有对堆上存在的任何对象实例的引用。

您要么从未为该变量分配过任何内容,也从未创建过分配给该变量的值的实例,要么您手动将变量设置为 Nothing/null,或者您调用了一个函数,为您将变量设置为 Nothing/null

Be aware that regardless of the scenario, the cause is always the same in .NET:

You are trying to use a reference variable whose value is Nothing/null. When the value is Nothing/null for the reference variable, that means it is not actually holding a reference to an instance of any object that exists on the heap.

You either never assigned something to the variable, never created an instance of the value assigned to the variable, or you set the variable equal to Nothing/null manually, or you called a function that set the variable to Nothing/null for you.

万劫不复 2025-01-24 18:31:01

抛出此异常的一个示例是:当您尝试检查某些内容时,该内容为空。

例如:

string testString = null; //Because it doesn't have a value (i.e. it's null; "Length" cannot do what it needs to do)

if (testString.Length == 0) // Throws a nullreferenceexception
{
    //Do something
} 

当您尝试对尚未实例化的内容(即上面的代码)执行操作时,.NET 运行时将抛出 NullReferenceException。

与 ArgumentNullException 相比,如果方法期望传递给它的内容不为空,则通常将抛出 ArgumentNullException 作为防御措施。

更多信息请参阅C# NullReferenceException 和 Null 参数

An example of this exception being thrown is: When you are trying to check something, that is null.

For example:

string testString = null; //Because it doesn't have a value (i.e. it's null; "Length" cannot do what it needs to do)

if (testString.Length == 0) // Throws a nullreferenceexception
{
    //Do something
} 

The .NET runtime will throw a NullReferenceException when you attempt to perform an action on something which hasn't been instantiated i.e. the code above.

In comparison to an ArgumentNullException which is typically thrown as a defensive measure if a method expects that what is being passed to it is not null.

More information is in C# NullReferenceException and Null Parameter.

终难遇 2025-01-24 18:31:01

更新 C#8.0,2019:可为空引用类型

C#8.0 引入了可为空引用类型不可为空引用类型。因此,必须仅检查可为 null 的引用类型以避免 NullReferenceException


如果您尚未初始化引用类型,并且想要设置或读取其属性之一,则会抛出 NullReferenceException

示例:

Person p = null;
p.Name = "Harry"; // NullReferenceException occurs here.

您可以通过检查变量是否不为空来简单地避免这种情况:

Person p = null;
if (p!=null)
{
    p.Name = "Harry"; // Not going to run to this point
}

要完全理解为什么抛出 NullReferenceException,了解 值类型 和 [引用类型][3]。

因此,如果您正在处理值类型,则可能不会发生 NullReferenceException。不过在处理引用类型时您需要保持警惕!

顾名思义,只有引用类型可以保存引用或实际上指向任何内容(或“null”)。而值类型总是包含一个值。

引用类型(必须勾选这些):

  • 动态
  • 对象
  • 字符串

值类型(可以忽略这些):

  • 数字类型
  • 整数类型
  • 浮点类型
  • 小数
  • bool
  • 用户定义结构体

Update C#8.0, 2019: Nullable reference types

C#8.0 introduces nullable reference types and non-nullable reference types. So only nullable reference types must be checked to avoid a NullReferenceException.


If you have not initialized a reference type, and you want to set or read one of its properties, it will throw a NullReferenceException.

Example:

Person p = null;
p.Name = "Harry"; // NullReferenceException occurs here.

You can simply avoid this by checking if the variable is not null:

Person p = null;
if (p!=null)
{
    p.Name = "Harry"; // Not going to run to this point
}

To fully understand why a NullReferenceException is thrown, it is important to know the difference between value types and [reference types][3].

So, if you're dealing with value types, NullReferenceExceptions can not occur. Though you need to keep alert when dealing with reference types!

Only reference types, as the name is suggesting, can hold references or point literally to nothing (or 'null'). Whereas value types always contain a value.

Reference types (these ones must be checked):

  • dynamic
  • object
  • string

Value types (you can simply ignore these ones):

  • Numeric types
  • Integral types
  • Floating-point types
  • decimal
  • bool
  • User defined structs
飘然心甜 2025-01-24 18:31:01

nullReferenceExceptions 可能发生的另一种情况是(不正确)使用 as 操作员

class Book {
    public string Name { get; set; }
}
class Car { }

Car mycar = new Car();
Book mybook = mycar as Book;   // Incompatible conversion --> mybook = null

Console.WriteLine(mybook.Name);   // NullReferenceException

在此, book and car 是不兼容的类型; CAR 无法转换/铸造到 book 。当此演员失败时,返回 null 。使用 mybook 在导致 nullReferenceException 之后。

通常,您应该使用cast或作为,如下:

如果您期望类型转换始终成功(即。演员:

ComicBook cb = (ComicBook)specificBook;

如果您不确定类型,但是要尝试将其用作特定类型,然后将用作

ComicBook cb = specificBook as ComicBook;
if (cb != null) {
   // ...
}

Another case where NullReferenceExceptions can happen is the (incorrect) use of the as operator:

class Book {
    public string Name { get; set; }
}
class Car { }

Car mycar = new Car();
Book mybook = mycar as Book;   // Incompatible conversion --> mybook = null

Console.WriteLine(mybook.Name);   // NullReferenceException

Here, Book and Car are incompatible types; a Car cannot be converted/cast to a Book. When this cast fails, as returns null. Using mybook after this causes a NullReferenceException.

In general, you should use a cast or as, as follows:

If you are expecting the type conversion to always succeed (ie. you know what the object should be ahead of time), then you should use a cast:

ComicBook cb = (ComicBook)specificBook;

If you are unsure of the type, but you want to try to use it as a specific type, then use as:

ComicBook cb = specificBook as ComicBook;
if (cb != null) {
   // ...
}
盗心人 2025-01-24 18:31:01

您正在使用包含空值引用的对象。所以它给出了一个空异常。在示例中,字符串值为 null,在检查其长度时发生异常。

示例:

string value = null;
if (value.Length == 0) // <-- Causes exception
{
    Console.WriteLine(value); // <-- Never reached
}

异常错误为:

未处理的异常:

System.NullReferenceException:未将对象引用设置到实例
一个物体的。在 Program.Main()

You are using the object that contains the null value reference. So it's giving a null exception. In the example the string value is null and when checking its length, the exception occurred.

Example:

string value = null;
if (value.Length == 0) // <-- Causes exception
{
    Console.WriteLine(value); // <-- Never reached
}

The exception error is:

Unhandled Exception:

System.NullReferenceException: Object reference not set to an instance
of an object. at Program.Main()

恋竹姑娘 2025-01-24 18:31:01

什么导致NullReferenceExceptions 以及避免/修复此类异常的方法已在其他答案中得到解决,许多程序员尚未学会的是如何独立调试此类异常期间的例外情况 发展。

在 Visual Studio 中,借助 Visual Studio 调试器,这通常很容易。


首先,确保将捕获正确的错误 - 请参阅
如何允许在 VS2010 中中断“System.NullReferenceException”? 注意1< /em>

然后或者 开始调试 (F5 )将[VS 调试器]附加到正在运行的进程。有时使用 Debugger.Break,这将提示启动调试器。

现在,当抛出(或未处理)NullReferenceException 时,调试器将在发生异常的行上停止(还记得上面设置的规则吗?)。有时错误很容易被发现。

例如,
在下面的行中,可能导致异常的唯一代码是如果 myString 计算结果为 null。这可以通过查看观察窗口 或在 立即中运行表达式窗口

var x = myString.Trim();

在更高级的情况下,例如以下情况,您需要使用上述技术之一(Watch 或 Immediate Windows)来检查表达式以确定 str1 是否为 null 或者 str2 为空。

var x = str1.Trim() + str2.Trim();

一旦定位了抛出异常的位置,通常很容易向后推理以找出[错误]引入空值的位置 -

花一些时间来了解异常的原因。检查空表达式。检查可能导致此类空表达式的先前表达式。添加断点 并根据需要逐步执行该程序。 使用调试器。


1 如果 Break on Throws 过于激进并且调试器在 .NET 或第 3 方库中的 NPE 上停止,Break on User-Unhandled 可用于限制捕获的异常。此外,VS2012 引入了Just My Code,我也建议启用它。

如果您在启用“仅我的代码”的情况下进行调试,则行为会略有不同。启用“仅我的代码”后,调试器会忽略在“我的代码”之外引发且不通过“我的代码”的首次公共语言运行时 (CLR) 异常

While what causes a NullReferenceExceptions and approaches to avoid/fix such an exception have been addressed in other answers, what many programmers haven't learned yet is how to independently debug such exceptions during development.

In Visual Studio this is usually easy thanks to the Visual Studio Debugger.


First, make sure that the correct error is going to be caught - see
How do I allow breaking on 'System.NullReferenceException' in VS2010? Note1

Then either Start with Debugging (F5) or Attach [the VS Debugger] to Running Process. On occasion it may be useful to use Debugger.Break, which will prompt to launch the debugger.

Now, when the NullReferenceException is thrown (or unhandled) the debugger will stop (remember the rule set above?) on the line on which the exception occurred. Sometimes the error will be easy to spot.

For instance,
in the following line the only code that can cause the exception is if myString evaluates to null. This can be verified by looking at the Watch Window or running expressions in the Immediate Window.

var x = myString.Trim();

In more advanced cases, such as the following, you'll need to use one of the techniques above (Watch or Immediate Windows) to inspect the expressions to determine if str1 was null or if str2 was null.

var x = str1.Trim() + str2.Trim();

Once where the exception is throw has been located, it's usually trivial to reason backwards to find out where the null value was [incorrectly] introduced --

Take the time required to understand the cause of the exception. Inspect for null expressions. Inspect the previous expressions which could have resulted in such null expressions. Add breakpoints and step through the program as appropriate. Use the debugger.


1 If Break on Throws is too aggressive and the debugger stops on an NPE in the .NET or 3rd-party library, Break on User-Unhandled can be used to limit the exceptions caught. Additionally, VS2012 introduces Just My Code which I recommend enabling as well.

If you are debugging with Just My Code enabled, the behavior is slightly different. With Just My Code enabled, the debugger ignores first-chance common language runtime (CLR) exceptions that are thrown outside of My Code and do not pass through My Code

有深☉意 2025-01-24 18:31:01

Simon Mourier给了这个示例

object o = null;
DateTime d = (DateTime)o;  // NullReferenceException

其中 unboxing 转换(铸造(铸造) )来自 object (或从类别之一 system.valuetype system.enum ,或从接口type) to 一个值类型( nullable&lt;&gt; )本身给出了 nullReferenceException

在另一个方向上,来自 a a nullable&lt;&gt;&gt; 拳击 转换代码>等于 false to 参考类型,可以给出 null 参考,然后可以稍后引入 NullReferenceException 。经典的例子是:

DateTime? d = null;
var s = d.ToString();  // OK, no exception (no boxing), returns ""
var t = d.GetType();   // Bang! d is boxed, NullReferenceException

有时拳击会以另一种方式发生。例如,使用此非传播扩展方法:

public static void MyExtension(this object x)
{
  x.ToString();
}

以下代码将是有问题的:

DateTime? d = null;
d.MyExtension();  // Leads to boxing, NullReferenceException occurs inside the body of the called method, not here.

这些情况是由于运行时在拳击 nullable时使用的特殊规则而出现的。

Simon Mourier gave this example:

object o = null;
DateTime d = (DateTime)o;  // NullReferenceException

where an unboxing conversion (cast) from object (or from one of the classes System.ValueType or System.Enum, or from an interface type) to a value type (other than Nullable<>) in itself gives the NullReferenceException.

In the other direction, a boxing conversion from a Nullable<> which has HasValue equal to false to a reference type, can give a null reference which can then later lead to a NullReferenceException. The classic example is:

DateTime? d = null;
var s = d.ToString();  // OK, no exception (no boxing), returns ""
var t = d.GetType();   // Bang! d is boxed, NullReferenceException

Sometimes the boxing happens in another way. For example with this non-generic extension method:

public static void MyExtension(this object x)
{
  x.ToString();
}

the following code will be problematic:

DateTime? d = null;
d.MyExtension();  // Leads to boxing, NullReferenceException occurs inside the body of the called method, not here.

These cases arise because of the special rules the runtime uses when boxing Nullable<> instances.

罗罗贝儿 2025-01-24 18:31:01

当实体框架中使用的实体的类名称与Web表单代码范围文件的类名称相同时,添加一个案例。

假设您有一个Web表单Contact.ASPX,其CodeBehind类是联系人,并且您具有实体名称联系人。

请在调用Context.SaveChanges()时,以下代码将丢弃NullReferenceException

Contact contact = new Contact { Name = "Abhinav"};
var context = new DataContext();
context.Contacts.Add(contact);
context.SaveChanges(); // NullReferenceException at this line

然后,为了完整的dataContext类

public class DataContext : DbContext 
{
    public DbSet<Contact> Contacts {get; set;}
}

和联系人实体类, 。有时,实体类是部分类,因此您也可以在其他文件中扩展它们。

public partial class Contact 
{
    public string Name {get; set;}
}

当实体和CodeBehind类处于同一名称空间时,发生错误。
要解决此问题,请重命名实体类或CodeBehind类以供contact.aspx。

原因
我仍然不确定原因。但是,每当任何实体类都扩展system.web.ui.page时,此错误就会发生。

为了讨论,请查看 nullReReference exception in dbcontext.savechanges()

Adding a case when the class name for entity used in entity framework is same as class name for a web form code-behind file.

Suppose you have a web form Contact.aspx whose codebehind class is Contact and you have an entity name Contact.

Then following code will throw a NullReferenceException when you call context.SaveChanges()

Contact contact = new Contact { Name = "Abhinav"};
var context = new DataContext();
context.Contacts.Add(contact);
context.SaveChanges(); // NullReferenceException at this line

For the sake of completeness DataContext class

public class DataContext : DbContext 
{
    public DbSet<Contact> Contacts {get; set;}
}

and Contact entity class. Sometimes entity classes are partial classes so that you can extend them in other files too.

public partial class Contact 
{
    public string Name {get; set;}
}

The error occurs when both the entity and codebehind class are in same namespace.
To fix this, rename the entity class or the codebehind class for Contact.aspx.

Reason
I am still not sure about the reason. But whenever any of the entity class will extend System.Web.UI.Page this error occurs.

For discussion have a look at NullReferenceException in DbContext.saveChanges()

爱已欠费 2025-01-24 18:31:01

另一个可能会收到此例外的常规情况涉及在单元测试期间嘲笑课程。无论使用的模拟框架如何,您都必须确保正确嘲笑所有适当的班级层次结构。特别是,必须模拟 httpContext 的所有属性。

请参阅“ nullReReferenizAttribute> nullReReferenizectexception在测试自定义授权attribute 时,就会丢弃。

Another general case where one might receive this exception involves mocking classes during unit testing. Regardless of the mocking framework being used, you must ensure that all appropriate levels of the class hierarchy are properly mocked. In particular, all properties of HttpContext which are referenced by the code under test must be mocked.

See "NullReferenceException thrown when testing custom AuthorizationAttribute" for a somewhat verbose example.

流绪微梦 2025-01-24 18:31:01

我对这个问题有不同的看法。这种答案“我还能做什么来避免它?

跨不同层工作时,例如在 MVC 应用程序中,控制器需要服务来调用业务操作。在这种情况下,可以使用依赖注入容器来初始化服务以避免NullReferenceException。因此,这意味着您无需担心检查 null,只需从控制器调用服务,就好像它们始终作为单例或原型可用(并初始化)一样。

public class MyController
{
    private ServiceA serviceA;
    private ServiceB serviceB;

    public MyController(ServiceA serviceA, ServiceB serviceB)
    {
        this.serviceA = serviceA;
        this.serviceB = serviceB;
    }

    public void MyMethod()
    {
        // We don't need to check null because the dependency injection container 
        // injects it, provided you took care of bootstrapping it.
        var someObject = serviceA.DoThis();
    }
}

I have a different perspective to answering this. This sort of answers "what else can I do to avoid it?"

When working across different layers, for example in an MVC application, a controller needs services to call business operations. In such scenarios Dependency Injection Container can be used to initialize the services to avoid the NullReferenceException. So that means you don't need to worry about checking for null and just call the services from the controller as though they will always to available (and initialized) as either a singleton or a prototype.

public class MyController
{
    private ServiceA serviceA;
    private ServiceB serviceB;

    public MyController(ServiceA serviceA, ServiceB serviceB)
    {
        this.serviceA = serviceA;
        this.serviceB = serviceB;
    }

    public void MyMethod()
    {
        // We don't need to check null because the dependency injection container 
        // injects it, provided you took care of bootstrapping it.
        var someObject = serviceA.DoThis();
    }
}
﹏半生如梦愿梦如真 2025-01-24 18:31:01

关于“我该怎么办”这个问题,可以有很多答案。

在开发时防止此类错误情况的一种更“正式”的方法是应用在代码中按契约设计。这意味着您需要在开发时在系统上设置类不变量,和/或什至函数/方法前置条件后置条件

简而言之,类不变量确保您的类中存在一些在正常使用中不会被违反的约束(因此,该类不会处于不一致的状态)。 先决条件意味着作为函数/方法的输入给出的数据必须遵循一些约束集并且决不违反它们,而后置条件意味着函数/方法输出必须再次遵循设定的约束,而不能违反它们。
在执行无错误程序期间绝不违反合同条件,因此在调试模式下实际上会检查合同设计,同时在发布中禁用,以最大限度地提高开发的系统性能。

这样,您可以避免因违反约束集而导致的 NullReferenceException 情况。例如,如果您在类中使用对象属性 X,然后尝试调用其方法之一,而 X 具有 null 值,则这将导致 NullReferenceException

public X { get; set; }

public void InvokeX()
{
    X.DoSomething(); // if X value is null, you will get a NullReferenceException
}

但是,如果您将“属性 X 绝不能有空值”设置为方法前提条件,那么您可以防止前面描述的情况:

//Using code contracts:
[ContractInvariantMethod]
protected void ObjectInvariant() 
{
    Contract.Invariant(X != null);
    //...
}

因此,代码合同项目存在对于.NET应用程序。

或者,可以使用断言来应用按合同设计

更新:值得一提的是,该术语是由 Bertrand Meyer 创造的 与他设计的 Eiffel 编程语言有关

On the matter of "what should I do about it", there can be many answers.

A more "formal" way of preventing such error conditions while developing is applying design by contract in your code. This means you need to set class invariants, and/or even function/method preconditions and postconditions on your system, while developing.

In short, class invariants ensure that there will be some constraints in your class that will not get violated in normal use (and therefore, the class will not get in an inconsistent state). Preconditions mean that data given as input to a function/method must follow some constraints set and never violate them, and postconditions mean that a function/method output must follow the set constraints again without ever violating them.
Contract conditions should never be violated during execution of a bug-free program, therefore design by contract is checked in practice in debug mode, while being disabled in releases, to maximize the developed system performance.

This way, you can avoid NullReferenceException cases that are results of violation of the constraints set. For example, if you use an object property X in a class and later try to invoke one of its methods and X has a null value, then this will lead to NullReferenceException:

public X { get; set; }

public void InvokeX()
{
    X.DoSomething(); // if X value is null, you will get a NullReferenceException
}

But if you set "property X must never have a null value" as method precondition, then you can prevent the scenario described before:

//Using code contracts:
[ContractInvariantMethod]
protected void ObjectInvariant() 
{
    Contract.Invariant(X != null);
    //...
}

For this cause, Code Contracts project exists for .NET applications.

Alternatively, design by contract can be applied using assertions.

UPDATE: It is worth mentioning that the term was coined by Bertrand Meyer in connection with his design of the Eiffel programming language.

自控 2025-01-24 18:31:01

当我们试图访问空对象的属性或字符串值变为空并且我们正在尝试访问字符串方法时, nullReferenceException 是抛出的。

例如:

  1. 访问空字符串的字符串方法:

     字符串str = string.empty;
    str.tolower(); //投掷null参考异常
     
  2. 当访问空对象的属性时:

     公共班级人{
        公共字符串名称{get;放; }
    }
    人objperson;
    objperson.name /// fort null refernce异常 
     

A NullReferenceException is thrown when we are trying to access Properties of a null object or when a string value becomes empty and we are trying to access string methods.

For example:

  1. When a string method of an empty string accessed:

    string str = string.Empty;
    str.ToLower(); // throw null reference exception
    
  2. When a property of a null object accessed:

    Public Class Person {
        public string Name { get; set; }
    }
    Person objPerson;
    objPerson.Name  /// throw Null refernce Exception 
    
兰花执着 2025-01-24 18:31:01

TL;DR:尝试使用Html.Partial而不是Renderpage


我得到对象引用未设置到对象的实例< /code> 当我尝试通过向视图发送模型来渲染视图中的视图时,如下所示:

@{
    MyEntity M = new MyEntity();
}
@RenderPage("_MyOtherView.cshtml", M); // error in _MyOtherView, the Model was Null

调试显示模型在 MyOtherView 中为 Null。直到我将其更改为:

@{
    MyEntity M = new MyEntity();
}
@Html.Partial("_MyOtherView.cshtml", M);

并且它起作用了。

此外,我一开始就没有 Html.Partial 的原因是 Visual Studio 有时Html.Partial 下抛出看起来错误的波浪线> 如果它位于不同构造的 foreach 循环内,即使它并不是真正的错误:

@inherits System.Web.Mvc.WebViewPage
@{
    ViewBag.Title = "Entity Index";
    List<MyEntity> MyEntities = new List<MyEntity>();
    MyEntities.Add(new MyEntity());
    MyEntities.Add(new MyEntity());
    MyEntities.Add(new MyEntity());
}
<div>
    @{
        foreach(var M in MyEntities)
        {
            // Squiggly lines below. Hovering says: cannot convert method group 'partial' to non-delegate type Object, did you intend to envoke the Method?
            @Html.Partial("MyOtherView.cshtml");
        }
    }
</div>

但我能够运行该应用程序,而不会出现此“错误”的问题。我能够通过将 foreach 循环的结构更改为如下所示来消除该错误:

@foreach(var M in MyEntities){
    ...
}

虽然我有一种感觉,这是因为 Visual Studio 误读了 & 符号和括号。

TL;DR: Try using Html.Partial instead of Renderpage


I was getting Object reference not set to an instance of an object when I tried to render a View within a View by sending it a Model, like this:

@{
    MyEntity M = new MyEntity();
}
@RenderPage("_MyOtherView.cshtml", M); // error in _MyOtherView, the Model was Null

Debugging showed the model was Null inside MyOtherView. Until I changed it to:

@{
    MyEntity M = new MyEntity();
}
@Html.Partial("_MyOtherView.cshtml", M);

And it worked.

Furthermore, the reason I didn't have Html.Partial to begin with was because Visual Studio sometimes throws error-looking squiggly lines under Html.Partial if it's inside a differently constructed foreach loop, even though it's not really an error:

@inherits System.Web.Mvc.WebViewPage
@{
    ViewBag.Title = "Entity Index";
    List<MyEntity> MyEntities = new List<MyEntity>();
    MyEntities.Add(new MyEntity());
    MyEntities.Add(new MyEntity());
    MyEntities.Add(new MyEntity());
}
<div>
    @{
        foreach(var M in MyEntities)
        {
            // Squiggly lines below. Hovering says: cannot convert method group 'partial' to non-delegate type Object, did you intend to envoke the Method?
            @Html.Partial("MyOtherView.cshtml");
        }
    }
</div>

But I was able to run the application with no problems with this "error". I was able to get rid of the error by changing the structure of the foreach loop to look like this:

@foreach(var M in MyEntities){
    ...
}

Although I have a feeling it was because Visual Studio was misreading the ampersands and brackets.

陪你搞怪i 2025-01-24 18:31:01

你能对此做什么?

这里有很多很好的答案,解释了什么是空引用以及如何调试它。但关于如何预防该问题或至少使其更容易被发现的信息却很少。

检查参数

例如,方法可以检查不同的参数以查看它们是否为 null 并抛出 ArgumentNullException,显然是为此目的而创建的异常。

ArgumentNullException 的构造函数甚至将参数名称和消息作为参数,以便您可以准确地告诉开发人员问题所在。

public void DoSomething(MyObject obj) {
    if(obj == null) 
    {
        throw new ArgumentNullException("obj", "Need a reference to obj.");
    }
}

使用工具

还有一些库可以提供帮助。例如,“Resharper”可以在您编写代码时向您提供警告,特别是在您使用其属性时: NotNullAttribute

有“Microsoft 代码合同”,您可以使用像 Contract.Requires(obj != null) 这样的语法,它可以为您提供运行时和编译检查:介绍代码契约

还有“PostSharp”,它允许您仅使用如下属性:

public void DoSometing([NotNull] obj)

通过这样做并使 PostSharp 成为构建过程的一部分,将在运行时检查 obj 是否为 null。请参阅:PostSharp 空值检查

纯代码解决方案

或者您始终可以使用纯旧代码编写自己的方法。例如,这是一个可用于捕获空引用的结构。它采用与 Nullable 相同的概念进行建模:

[System.Diagnostics.DebuggerNonUserCode]
public struct NotNull<T> where T: class
{
    private T _value;

    public T Value
    {
        get
        {
            if (_value == null)
            {
                throw new Exception("null value not allowed");
            }

            return _value;
        }
        set
        {
            if (value == null)
            {
                throw new Exception("null value not allowed.");
            }

            _value = value;
        }
    }

    public static implicit operator T(NotNull<T> notNullValue)
    {
        return notNullValue.Value;
    }

    public static implicit operator NotNull<T>(T value)
    {
        return new NotNull<T> { Value = value };
    }
}

您的使用方式与使用 Nullable 的方式非常相似,只不过目标是准确完成相反 - 不允许 null。以下是一些示例:

NotNull<Person> person = null; // throws exception
NotNull<Person> person = new Person(); // OK
NotNull<Person> person = GetPerson(); // throws exception if GetPerson() returns null

NotNullT 之间隐式转换,因此您可以在任何需要的地方使用它。例如,您可以将 Person 对象传递给采用 NotNull 的方法:

Person person = new Person { Name = "John" };
WriteName(person);

public static void WriteName(NotNull<Person> person)
{
    Console.WriteLine(person.Value.Name);
}

正如您在上面所看到的,对于可为 null 的情况,您可以通过Value 属性。或者,您可以使用显式或隐式转换,您可以看到下面带有返回值的示例:

Person person = GetPerson();

public static NotNull<Person> GetPerson()
{
    return new Person { Name = "John" };
}

或者您甚至可以在方法仅返回 T 时使用它(在本例中为 Person< /code>) 通过进行强制转换。例如,以下代码与上面的代码类似:

Person person = (NotNull<Person>)GetPerson();

public static Person GetPerson()
{
    return new Person { Name = "John" };
}

与扩展结合

NotNull 与扩展方法结合,您可以覆盖更多情况。以下是扩展方法的示例:

[System.Diagnostics.DebuggerNonUserCode]
public static class NotNullExtension
{
    public static T NotNull<T>(this T @this) where T: class
    {
        if (@this == null)
        {
            throw new Exception("null value not allowed");
        }

        return @this;
    }
}

以下是如何使用它的示例:

var person = GetPerson().NotNull();

GitHub

为了供您参考,我在 GitHub 上提供了上述代码,您可以在以下位置找到它:

https://github.com/luisperezphd/NotNull

相关语言功能

C# 6.0 引入了“null-conditional”运算符”这对此有一点帮助。利用此功能,您可以引用嵌套对象,如果其中任何一个为 null,则整个表达式将返回 null

这减少了在某些情况下必须执行的 null 检查的数量。语法是在每个点之前放置一个问号。以下面的代码为例:

var address = country?.State?.County?.City;

假设country是一个Country类型的对象,它有一个名为State的属性等等。如果 countryStateCountyCitynull地址将为null。因此,您只需检查address是否为null`。

这是一个很棒的功能,但它为您提供的信息较少。它并没有明确表明这 4 个中哪一个为空。

像 Nullable 一样内置?

C# 有一个很好的 Nullable 简写方式,您可以通过在类型后面添加一个问号来使某些内容可以为 null,如 int ?

如果 C# 具有类似于上面的 NotNull 结构的内容,并且具有类似的简写形式(可能是感叹号 (!)),那就太好了,这样您就可以编写类似以下内容的内容: public void WriteName (人!人)

What can you do about it?

There is a lot of good answers here explaining what a null reference is and how to debug it. But there is very little on how to prevent the issue or at least make it easier to catch.

Check arguments

For example, methods can check the different arguments to see if they are null and throw an ArgumentNullException, an exception obviously created for this exact purpose.

The constructor for the ArgumentNullException even takes the name of the parameter and a message as arguments so you can tell the developer exactly what the problem is.

public void DoSomething(MyObject obj) {
    if(obj == null) 
    {
        throw new ArgumentNullException("obj", "Need a reference to obj.");
    }
}

Use Tools

There are also several libraries that can help. "Resharper" for example can provide you with warnings while you are writing code, especially if you use their attribute: NotNullAttribute

There's "Microsoft Code Contracts" where you use syntax like Contract.Requires(obj != null) which gives you runtime and compile checking: Introducing Code Contracts.

There's also "PostSharp" which will allow you to just use attributes like this:

public void DoSometing([NotNull] obj)

By doing that and making PostSharp part of your build process obj will be checked for null at runtime. See: PostSharp null check

Plain Code Solution

Or you can always code your own approach using plain old code. For example here is a struct that you can use to catch null references. It's modeled after the same concept as Nullable<T>:

[System.Diagnostics.DebuggerNonUserCode]
public struct NotNull<T> where T: class
{
    private T _value;

    public T Value
    {
        get
        {
            if (_value == null)
            {
                throw new Exception("null value not allowed");
            }

            return _value;
        }
        set
        {
            if (value == null)
            {
                throw new Exception("null value not allowed.");
            }

            _value = value;
        }
    }

    public static implicit operator T(NotNull<T> notNullValue)
    {
        return notNullValue.Value;
    }

    public static implicit operator NotNull<T>(T value)
    {
        return new NotNull<T> { Value = value };
    }
}

You would use very similar to the same way you would use Nullable<T>, except with the goal of accomplishing exactly the opposite - to not allow null. Here are some examples:

NotNull<Person> person = null; // throws exception
NotNull<Person> person = new Person(); // OK
NotNull<Person> person = GetPerson(); // throws exception if GetPerson() returns null

NotNull<T> is implicitly cast to and from T so you can use it just about anywhere you need it. For example, you can pass a Person object to a method that takes a NotNull<Person>:

Person person = new Person { Name = "John" };
WriteName(person);

public static void WriteName(NotNull<Person> person)
{
    Console.WriteLine(person.Value.Name);
}

As you can see above as with nullable you would access the underlying value through the Value property. Alternatively, you can use an explicit or implicit cast, you can see an example with the return value below:

Person person = GetPerson();

public static NotNull<Person> GetPerson()
{
    return new Person { Name = "John" };
}

Or you can even use it when the method just returns T (in this case Person) by doing a cast. For example, the following code would just like the code above:

Person person = (NotNull<Person>)GetPerson();

public static Person GetPerson()
{
    return new Person { Name = "John" };
}

Combine with Extension

Combine NotNull<T> with an extension method and you can cover even more situations. Here is an example of what the extension method can look like:

[System.Diagnostics.DebuggerNonUserCode]
public static class NotNullExtension
{
    public static T NotNull<T>(this T @this) where T: class
    {
        if (@this == null)
        {
            throw new Exception("null value not allowed");
        }

        return @this;
    }
}

And here is an example of how it could be used:

var person = GetPerson().NotNull();

GitHub

For your reference I made the code above available on GitHub, you can find it at:

https://github.com/luisperezphd/NotNull

Related Language Feature

C# 6.0 introduced the "null-conditional operator" that helps with this a little. With this feature, you can reference nested objects and if any one of them is null the whole expression returns null.

This reduces the number of null checks you have to do in some cases. The syntax is to put a question mark before each dot. Take the following code for example:

var address = country?.State?.County?.City;

Imagine that country is an object of type Country that has a property called State and so on. If country, State, County, or City is null then address will benull. Therefore you only have to check whetheraddressisnull`.

It's a great feature, but it gives you less information. It doesn't make it obvious which of the 4 is null.

Built-in like Nullable?

C# has a nice shorthand for Nullable<T>, you can make something nullable by putting a question mark after the type like so int?.

It would be nice if C# had something like the NotNull<T> struct above and had a similar shorthand, maybe the exclamation point (!) so that you could write something like: public void WriteName(Person! person).

桜花祭 2025-01-24 18:31:01

您可以使用c#6中的无条件运算符以干净的方式修复 NullReferenceException ,并编写较少的代码来处理无效检查。

它用于在执行成员访问(?)或索引(?[)操作之前测试NULL。

示例

  var name = p?.Spouse?.FirstName;

等于:

    if (p != null)
    {
        if (p.Spouse != null)
        {
            name = p.Spouse.FirstName;
        }
    }

结果是,当p为null或 p.p.spouse 为null时,名称将为null。

否则,将分配变量名称 p.spouse.firstname 的值。

对于更多详细信息: null条件运算符

You can fix NullReferenceException in a clean way using Null-conditional Operators in C# 6 and write less code to handle null checks.

It's used to test for null before performing a member access (?.) or index (?[) operation.

Example

  var name = p?.Spouse?.FirstName;

It is equivalent to:

    if (p != null)
    {
        if (p.Spouse != null)
        {
            name = p.Spouse.FirstName;
        }
    }

The result is that the name will be null when p is null or when p.Spouse is null.

Otherwise, the variable name will be assigned the value of the p.Spouse.FirstName.

For more details: Null-conditional Operators

§对你不离不弃 2025-01-24 18:31:01

有趣的是,此页面上没有一个答案提及两个边缘情况:

边缘案例#1:同时访问

.NET中对字典通用词典的访问不是线程安全,并且它们有时可能会抛出 nullReference ,甚至(更频繁的) keynotfoundException 尝试从两个并发线程访问键时。在这种情况下,例外是非常误导的。

边缘案例#2:

如果 nullReferenceException Unsafe 代码抛出的不安全代码,您可以查看指针变量,并检查它们是否 intptr.Zero 或其他东西。这是同一件事(“ null指针异常”),但是在不安全的代码中,变量通常被铸成值类型/数组等,并且您的头撞到了墙上,想知道价值类型如何将其扔给例外。

(顺便说一句,除非需要,除非您需要不使用不安全的代码。)

边缘案例#3:带有辅助监视器的Visual Studio Multi Monitor设置,该设置的DPI设置与主监视器不同,

此EDGE CASE是Software-与“ nofollow noreferrer”> Visualio&nbsp; studio&nbsp; 2019 iDe(以及可能的versions)。

一种重现问题的方法:将任何组件从工具箱拖到具有与主监视器不同的DPI设置的非主要监视器上的Windows表单中目的。”根据,这个问题已经闻名了很多时间,在编写时,它仍然没有修复。

Interestingly, none of the answers on this page mention the two edge cases:

Edge case #1: concurrent access to a Dictionary

Generic dictionaries in .NET are not thread-safe and they sometimes might throw a NullReference or even (more frequent) a KeyNotFoundException when you try to access a key from two concurrent threads. The exception is quite misleading in this case.

Edge case #2: unsafe code

If a NullReferenceException is thrown by unsafe code, you might look at your pointer variables, and check them for IntPtr.Zero or something. Which is the same thing ("null pointer exception"), but in unsafe code, variables are often cast to value-types/arrays, etc., and you bang your head against the wall, wondering how a value-type can throw this exception.

(Another reason for non-using unsafe code unless you need it, by the way.)

Edge case #3: Visual Studio multi monitor setup with secondary monitor(s) that has different DPI setting than the primary monitor

This edge case is software-specific and pertains to the Visual Studio 2019 IDE (and possibly earlier versions).

A method to reproduce the problem: drag any component from the Toolbox to a Windows form on a non-primary monitor with different DPI setting than the primary monitor, and you get a pop-up with “Object reference not set to an instance of an object.” According to this thread, this issue has been known for quite some time and at the time of writing it still hasn't been fixed.

缱倦旧时光 2025-01-24 18:31:01

错误行“未将对象引用设置到对象的实例。”表明您尚未将实例对象分配给对象引用,但您仍在访问该对象的属性/方法。

例如:假设您有一个名为 myClass 的类,它包含一个属性 prop1

public Class myClass
{
   public int prop1 {get;set;}
}

现在,您正在其他类中访问此 prop1,如下所示:

public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref.prop1 = 1;  // This line throws an error
     }
}

上面的行会抛出错误,因为声明了类 myClass 的引用,但未实例化,或者对象的实例未分配给该类的引用。

要解决此问题,您必须实例化(将对象分配给该类的引用)。

public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref = new myClass();
        ref.prop1 = 1;
     }
}

The error line "Object reference not set to an instance of an object." states that you have not assigned an instance object to a object reference and still you are accessing properties/methods of that object.

For example: let's say you have a class called myClass and it contains one property, prop1.

public Class myClass
{
   public int prop1 {get;set;}
}

Now you are accessing this prop1 in some other class just like below:

public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref.prop1 = 1;  // This line throws an error
     }
}

The above line throws an error because reference of class myClass is declared, but not instantiated or an instance of object is not assigned to a reference of that class.

To fix this you have to instantiate (assign an object to a reference of that class).

public class Demo
{
     public void testMethod()
     {
        myClass ref = null;
        ref = new myClass();
        ref.prop1 = 1;
     }
}
肤浅与狂妄 2025-01-24 18:31:01

好吧,简单地说:

您正在尝试访问未创建或当前未在内存中的对象。

因此,如何解决此问题:

  1. 调试并让调试器中断...它将直接带您进入损坏的变量...现在您的任务就是简单地解决此问题。.使用 new new 在适当位置的关键字。

  2. 如果它是在某些数据库命令上引起的,因为对象不存在,那么您要做的就是进行null检查并处理:

     如果(i == null){
        //处理这个
    }
     
  3. 最难的一个..如果 gc 已经收集了该对象...如果您想使用字符串找到一个对象,则通常会发生这种情况...也就是说,通过对象的名称找到它,那么GC可能已经将其清除了...这很难找到和将成为一个很大的问题...解决此问题的更好方法是在开发过程中必要的任何地方进行零检查。这将为您节省很多时间。

通过名称查找,我的意思是某个框架允许您使用字符串查找对象,并且代码可能看起来像: findObject(“ objectName”);

Well, in simple terms:

You are trying to access an object that isn't created or currently not in memory.

So how to tackle this:

  1. Debug and let the debugger break... It will directly take you to the variable that is broken... Now your task is to simply fix this.. Using the new keyword in the appropriate place.

  2. If it is caused on some database commands because the object isn't present then all you need to do is do a null check and handle it:

    if (i == null) {
        // Handle this
    }
    
  3. The hardest one .. if the GC collected the object already... This generally occurs if you are trying to find an object using strings... That is, finding it by name of the object then it may happen that the GC might already cleaned it up... This is hard to find and will become quite a problem... A better way to tackle this is do null checks wherever necessary during the development process. This will save you a lot of time.

By finding by name I mean some framework allow you to FIndObjects using strings and the code might look like this: FindObject("ObjectName");

゛时过境迁 2025-01-24 18:31:01

当您尝试使用的类的对象未实例化时,NullReferenceException或对象引用未设置为对象的实例。
例如:

假设您有一个名为Student的课程。

public class Student
{
    private string FirstName;
    private string LastName;
    public string GetFullName()
    {
        return FirstName + LastName;
    }
}

现在,考虑一下您试图检索学生全名的另一堂课。

public class StudentInfo
{      
    public string GetStudentName()
    {
        Student s;
        string fullname = s.GetFullName();
        return fullname;
    }        
}

如以上代码所示,该语句
学生S - 仅声明学生类型的变量,请注意,此时尚未实例化学生班。
因此,当语句 s.getfullname()被执行时,它将抛出NullReferenceException。

NullReferenceException or Object reference not set to an instance of an object occurs when an object of the class you are trying to use is not instantiated.
For example:

Assume that you have a class named Student.

public class Student
{
    private string FirstName;
    private string LastName;
    public string GetFullName()
    {
        return FirstName + LastName;
    }
}

Now, consider another class where you are trying to retrieve the student's full name.

public class StudentInfo
{      
    public string GetStudentName()
    {
        Student s;
        string fullname = s.GetFullName();
        return fullname;
    }        
}

As seen in the above code, the statement
Student s - only declares the variable of type Student, note that the Student class is not instantiated at this point.
Hence, when the statement s.GetFullName() gets executed, it will throw the NullReferenceException.

时间你老了 2025-01-24 18:31:01

从字面上看,修复nullReferenceExeption的最简单方法有两种方式。

如果您有一个gameObject,例如带有脚本的脚本和一个名为 rb (刚体)的变量,则该变量将在您启动游戏时以null开头。
这就是为什么您获得NullReferenceExeption的原因,因为计算机没有存储在该变量中的数据。

我将以一个刚性变量为例。
实际上,我们可以通过几种方式可以很容易地添加数据:

  1. 使用AddComponent&gt;向对象添加一个刚性体。物理&GT;僵化的身体
    然后进入您的脚本并键入 rb = getComponent&lt; strigbody&gt;();
    这一行代码在您的 start() awake()函数下工作。
  2. 您可以以编程方式添加组件,并同时使用一行代码分配变量: rb = addComponent&lt; strig&gt;();

进一步注释:如果您想要 unity 要在对象中添加一个组件,您可能已经忘记了一个,您可以键入一个 [sirceseComponent (typeof(arigidbody))] 在您的类声明上方(使用 s的所有下方的空间)。

享受并玩得开心!

Literally the easiest way to fix a NullReferenceExeption has two ways.

If you have a GameObject for example with a script attached and a variable named rb (rigidbody) this variable will start with null when you start your game.
This is why you get a NullReferenceExeption because the computer does not have data stored in that variable.

I'll be using a RigidBody variable as an example.
We can add data really easily actually in a few ways:

  1. Add a RigidBody to your object with AddComponent > Physics > Rigidbody
    Then go into your script and type rb = GetComponent<Rigidbody>();
    This line of code works best under your Start() or Awake() functions.
  2. You can add a component programmatically and assign the variable at the same time with one line of code: rb = AddComponent<RigidBody>();

Further Notes: If you want Unity to add a component to your object and you might have forgotten to add one, you can type [RequireComponent(typeof(RigidBody))] above your class declaration (the space below all of your usings).

Enjoy and have fun making games!

彡翼 2025-01-24 18:31:01

这基本上是一个零参考异常。 AS microsoft> Microsoft

尝试访问一个NullReference Exception异常
值为null的类型的成员。

这意味着什么?

这意味着,如果任何不具有任何价值的成员,我们正在使该成员执行某些任务,那么该系统无疑会扔出消息,然后说 -

“嘿,等等,该成员没有值,因此无法 的任务。

执行您要交出 因此,这表示仅在使用参考类型作为值类型时才发生。

如果我们使用Value类型成员,则不会发生NullReferenceException。

class Program
{
    static void Main(string[] args)
    {
        string str = null;
        Console.WriteLine(str.Length);
        Console.ReadLine();
    }
}

上面的代码显示了用 null 值分配的简单字符串。

现在,当我尝试打印字符串的长度 str 时,我确实会出现 'system.nullReferenceException'的类型消息,因为成员 str 消息/strong>指向零,没有任何时间的空。

' nullReferenceException '当我们忘记实例化参考类型时,也会发生。

假设我有一个类和成员方法。我没有实例化我的班级,但只命名了我的班级。现在,如果我尝试使用该方法,编译器将发出错误或发出警告(取决于编译器)。

class Program
{
    static void Main(string[] args)
    {
        MyClass1 obj;
        obj.foo();  // Use of unassigned local variable 'obj'
    }
}

public class MyClass1
{
    internal void foo()
    {
        Console.WriteLine("Hello from foo");
    }
}

上述代码的编译器提出了一个错误,即变量 obj 未分配,这表明我们的变量具有零值或没有。上述代码的编译器提出了一个错误,即变量 obj 未分配,这表明我们的变量具有零值或没有。

为什么发生?

  • nullReferenceException由于我们没有检查对象的值而出现了。我们经常将对象值放在代码开发中未选中。

  • 当我们忘记实例化对象时,它也会出现。使用方法,属性,集合等可以返回或设置为空值的方法也可能是此例外的原因。

如何避免它?

有多种避免此著名例外的方法和方法:

  1. 明确检查:我们应该遵守检查对象,属性,方法,阵列和集合的传统,无论它们是否为无效。可以简单地使用条件语句(例如if-else if-else等)实现。

  2. 异常处理:管理此例外的重要方法之一)来实现这一点。使用简单的尝试键入块,我们可以控制此异常并保持其日志。当您的应用程序处于生产阶段时,这可能非常有用。

  3. null运算符:无效的合并操作员和无条件操作员也可以用方便地使用,同时将值设置为对象,变量,属性和字段。

  4. 调试器:对于开发人员来说,我们拥有与我们一起调试的重要武器。如果我们在开发面前面对NullReferenceException,我们可以使用调试器来达到例外的来源。

  5. 内置方法:诸如getValueordEfault(),isnullorWhitespace()和isnullorement()检查nulls的系统方法,如果有空值。

    ,请分配默认值。

这里已经有很多好的答案。您还可以在我的博客

希望这也有帮助!

This is basically is a Null reference exception. As Microsoft states-

A NullReferenceException exception is thrown when you try to access a
member of a type whose value is null.

What does that mean?

That means if any member which doesn’t hold any value and we are making that member to perform certain task then the system will undoubtedly toss a message and say-

“Hey wait, that member has no values so it can’t perform the task which you are handing it over.”

The exception itself says that something is being referred but whose value is not being set. So this denotes that it only occurs while using reference types as Value types are non-nullable.

NullReferenceException won't occur if we are using Value type members.

class Program
{
    static void Main(string[] args)
    {
        string str = null;
        Console.WriteLine(str.Length);
        Console.ReadLine();
    }
}

The above code shows simple string which is assigned with a null value.

Now, when I try to print the length of the string str, I do get An unhandled exception of type ‘System.NullReferenceException’ occurred message because member str is pointing to null and there can’t be any length of null.

NullReferenceException’ also occurs when we forget to instantiate a reference type.

Suppose I have a class and member method in it. I have not instantiated my class but only named my class. Now if I try to use the method, the compiler will throw an error or issue a warning (depending on the compiler).

class Program
{
    static void Main(string[] args)
    {
        MyClass1 obj;
        obj.foo();  // Use of unassigned local variable 'obj'
    }
}

public class MyClass1
{
    internal void foo()
    {
        Console.WriteLine("Hello from foo");
    }
}

Compiler for the above code raises an error that variable obj is unassigned which signifies that our variable has null values or nothing. Compiler for the above code raises an error that variable obj is unassigned which signifies that our variable has null values or nothing.

Why does it occur?

  • NullReferenceException arises due to our fault for not checking the object’s value. We often leave the object values unchecked in the code development.

  • It also arises when we forget to instantiate our objects. Using methods, properties, collections etc. which can return or set null values can also be the cause of this exception.

How can it be avoided?

There are various ways and methods to avoid this renowned exception:

  1. Explicit Checking: We should adhere to the tradition of checking the objects, properties, methods, arrays, and collections whether they are null. This can be simply implemented using conditional statements like if-else if-else etc.

  2. Exception handling: One of the important ways of managing this exception. Using simple try-catch-finally blocks we can control this exception and also maintain a log of it. This can be very useful when your application is on production stage.

  3. Null operators: Null Coalescing operator and null conditional operators can also be used in handy while setting values to objects, variables, properties and fields.

  4. Debugger: For developers, we have the big weapon of Debugging with us. If have we face NullReferenceException during the development face we can use the debugger to get to the source of the exception.

  5. Built-in method: System methods such as GetValueOrDefault(), IsNullOrWhiteSpace(), and IsNullorEmpty() checks for nulls and assign the default value if there is a null value.

There are many good answers already here. You can also check more detailed description with examples on my blog.

Hope this helps too!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文