C#中的开发中,若是遇到“NullReferenceException”或者“未将对象引用到实例”这样的提示,那么是你的程序代码正在试图访问一个null的引用类型的实体而抛出的异常。可能的缘由有:数组
忘记了实例化一个引用类型。 在下面的示例中,names声明,但决不实例化:ide
using System; using System.Collections.Generic; public class Example { public static void Main(string[] args) { int value = Int32.Parse(args[0]); List<String> names; names.Add("Major Major Major"); } }
此例中的 names在使用以前并无初始化,修复后的:this
using System; using System.Collections.Generic; public class Example { public static void Main() { List<String> names = new List<String>(); names.Add("Major Major Major"); } }
以下代码:rest
ref1.ref2.ref3.member
若是ref1 或者 ref2 或者 ref3任意一个为空时,此代码均会抛出NullReferenceException(未将对象引用到实例)的异常错误。咱们能够重写这个表达式来检查如下的r1,r2,r3是否为null:code
var r1 = ref1; var r2 = r1.ref2; var r3 = r2.ref3; r3.member
以下代码:orm
public class Book { public string Title { get; set; } } public class Example { public void Foo() { Book b1; string title = b1.Title; } }
其中,Example类中的b1并未实例化,会抛出NullReferenceException(未将对象引用到实例)异常,解决方法:对象
使用new关键字来实例化b1对象
修复后的代码:生命周期
public class Book { public string Title { get; set; } } public class Example { public void Foo() { Book b1 = new Book(); string title = b1.Title; } }
以下代码:事件
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; } }
这里的 Example 类中的b1已被实例化,但若是咱们运行程序,依然会抛出NullReferenceException(未将对象引用到实例)异常。是由于 Book 类中包含了另一个引用类 Person 但并无被实例化,解决方式能够在Book的构造器中直接实例化,如:开发
public class Person { public int Age { get; set; } } public class Book { public Book(){ Author = new Person(); } public Person Author { get; set; } } public class Example { public void Foo() { Book b1 = new Book(); int authorAge = b1.Author.Age; } }
固然,你也能够在使用 Book 这个类的实例时再来初始化 Person 这个引用类,以下:
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(); b1.Author = new Person(); int authorAge = b1.Author.Age; } }
int[] numbers = null; int n = numbers[0];
Person[] people = new Person[5]; people[0].Age = 20
long[][] array = new long[1][]; array[0][0] = 3;
这三种数组的定义均为null,抛出NullReferenceException(未将对象引用到实例)的异常
Dictionary<string, int> agesForNames = null; int age = agesForNames["Bob"];
这里的 agesForNames字典为 null,抛出NullReferenceException(未将对象引用到实例)的异常,使用new关键字来初始化:
Dictionary<string, int> agesForNames = new Dictionary<string, int>(); int age = agesForNames["Bob"];
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();
以上代码会在 names.First() 处理抛出异常,由于咱们在
people.Add(null);
添加了null值
public class Demo { public event EventHandler StateChanged; protected virtual void OnStateChanged(EventArgs e) { StateChanged(this, e); } }
若是外部实例没有注册 StateChanged 事件,那么调用 StateChanged(this, e); 会抛出NullReferenceException(未将对象引用到实例),修复方法:
public class Demo { public event EventHandler StateChanged; protected virtual void OnStateChanged(EventArgs e) { if(StateChanged != null) { StateChanged(this, e); } } }
若是全局变量和局部变量的名称是同样的,那么你的全局变量就可能永远不会被赋值,以下:
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;
public partial class Issues_Edit : System.Web.UI.Page { protected TestIssue myIssue; protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // 只有页面首次加载时执行,点击按钮时不会被执行 myIssue = new TestIssue(); } } protected void SaveButton_Click(object sender, EventArgs e) { myIssue.Entry = "NullReferenceException here!"; } }
string firstName = Session["FirstName"].ToString();
若是Session["FirstName"]没有被赋值,则会抛出NullReferenceException(未将对象引用到实例)的异常。
// Controller[控制器] public class Restaurant:Controller { public ActionResult Search() { return View(); // Forgot the provide a Model here. } } // Razor[视图页面] @foreach (var restaurantSearch in Model.RestaurantSearch) //抛出异常 { } <p>@Model.somePropertyName</p> <!-- 抛出异常 -->
关于.NET[C#]中NullReferenceException(未将对象引用到实例)总结到这里,若是你遇到在不一样的情景遇到一样的异常,欢迎反馈、交流。