Tag: nullreferenceexception

引发/生成空引用exception背后的CLR实现是什么?

我们确实遇到过这个特殊问题,也是我们编码/开发生活日或其他日子中最常见的例外情况之一。 我的问题不是 为什么 (我知道当我们尝试访问实际指向null的引用变量的属性时它会引发),但它是关于CLR 如何生成NULL REFERENCE EXCEPTION。 有时我被迫认为用于标识对null的引用的机制(可能null是内存中的保留空间)然后通过CLR引发exception。 CLR如何识别并引发此特定exception。 操作系统在其中起任何作用吗? 我想与大家分享一个最有趣的主张: null实际上是CLR已知的所有时间保留的内存空间,并且禁止所有类型的访问。 因此,当找到该空间的引用时,它默认通过OS生成访问被拒绝的exception类型,CLR将其解释为NULL引用exception。 我没有找到支持上述陈述的任何文章或post,因此很难相信。 可能由于我缺少挖掘细节或其他原因,我希望Stackoverflow是最合适的平台之一,我会得到最好的响应。

你调用的对象是空的

我有一节课: public class Cell { public enum cellState { WATER, SCAN, SHIPUNIT, SHOT, HIT } public Cell() { currentCell = cellState.WATER; MessageBox.Show(currentCell.ToString()); } public cellState currentCell { get; set; } } 然后我尝试在以下类中使用它: public class NietzscheBattleshipsGameModel { private byte MAXCOL = 10; private byte MAXROW = 10; public Cell[,] HomeArray; private Cell[,] AwayArray; public NietzscheBattleshipsGameModel() { […]

在Linq中调用InsertOnSubmit到Sql时出现NullReferenceException

我正在尝试使用LINQ to SQL将新对象插入到我的数据库中,但是当我在下面的代码片段中调用InsertOnSubmit()时,会获得NullReferenceException。 我传入一个名为FileUploadAudit的派生类,并设置了该对象的所有属性。 public void Save(Audit audit) { try { using (ULNDataClassesDataContext dataContext = this.Connection.GetContext()) { if (audit.AuditID > 0) { throw new RepositoryException(RepositoryExceptionCode.EntityAlreadyExists, string.Format(“An audit entry with ID {0} already exists and cannot be updated.”, audit.AuditID)); } dataContext.Audits.InsertOnSubmit(audit); dataContext.SubmitChanges(); } } catch (Exception ex) { if (ObjectFactory.GetInstance().HandleException(ex)) { throw; } } } 这是堆栈跟踪: […]

Null在调用链中合并

如果我有一长串对象,每个对象都有可能在“Linq where”子句中返回null,例如 SomeSource.Where(srcItem=>(srcItem.DataMembers[“SomeText”].Connection.ConnectedTo as Type1).Handler.ForceInvocation == true)); 索引器可以返回null,“as”运算符可以返回null。 对象可能没有连接(即属性为null)。 如果在任何地方遇到null,我希望where子句为被评估的项返回“false”。 相反,它以null引用exception中止。 在我看来,这将被设计为在单个C#表达式中表达。 我不喜欢创建一个多行语句或为它创建一个单独的函数。 我是否缺少使用空合并运算符?

Html.OpenIdSelectorScripts抛出NullReferenceException的helper方法

尝试导航到LogOn页面时,我不断收到此错误: System.NullReferenceException:未将对象引用设置为对象的实例 抛出错误的代码行是: . 有谁知道为什么这行代码会抛出错误?

GetAdornerLayer神秘地返回null

我一直在为我的应用程序的几个版本使用相同的代码而没有任何问题,但我现在神秘地接收NullRerefenceException其中包含以下内容: this.Loaded += delegate { deleteBrush = new DeleteBrushAdorner( background ); AdornerLayer al = AdornerLayer.GetAdornerLayer( background ); al.Add( deleteBrush ); // null ref here?? }; background只是一个Border元素。 我对可能导致它的原因的两点看法是:a)切换到.NET 4.0,以及b)在ItemsControl放置上述元素的实例(这是一个UserControl )。 奇怪的是,这并不是一直发生的,很难预测何时会发生,所以它不可靠。

如何解决未将对象引用设置为对象的实例。

在我的asp.net程序中。我设置了一个受保护的列表。我在list中添加了一个值。但它显示对象引用未设置为对象错误的实例 protected List list; protected void Page_Load(object sender, EventArgs e) { list.Add(“hai”); } 如何解决这个错误?

如何只读静态字段为空?

所以这是我的一个课程的摘录: [ThreadStatic] readonly static private AccountManager _instance = new AccountManager(); private AccountManager() { } static public AccountManager Instance { get { return _instance; } } 如您所见,它是每个线程的单例 – 即该实例标有ThreadStatic属性。 该实例也被实例化为静态构造的一部分。 既然如此,当我尝试使用Instance属性时,我的ASP.NET MVC应用程序中是否有可能出现NullReferenceException?

如何在嵌套引用中检查null

寻找一些最佳实践指导。 假设我有一行代码如下: Color color = someOrder.Customer.LastOrder.Product.Color; 其中Customer,LastOrder,Product和Color在正常情况下可以为null 。 但是,如果路径中的任何一个对象为null,我希望color为null; 为了避免空引用exception,我需要检查每个对象的空条件,例如 Color color = someOrder == null || someOrder.Customer == null || someOrder.Customer.LastOrder == null || someOrder.Customer.Product == null ? null : someOrder.Customer.LastOrder.Product.Color; 或者我可以做到这一点 Color color = null; try {color = someOrder.Customer.LastOrder.Product.Color} catch (NullReferenceException) {} 第一种方法显然有效,但编码和更难阅读似乎更乏味。 第二种方法稍微容易一些,但对此使用exception处理可能不是一个好主意。 是否有其他快捷方式检查空值并在必要时将null指定为颜色? 或者在使用这种嵌套引用时如何避免NullReferenceExceptions的任何想法?

无法检测Session变量是否存在

我正在尝试确定Session变量是否存在,但我收到错误: System.NullReferenceException:未将对象引用设置为对象的实例。 码: // Check if the “company_path” exists in the Session context if (System.Web.HttpContext.Current.Session[“company_path”].ToString() != null) { // Session exists, set it company_path = System.Web.HttpContext.Current.Session[“company_path”].ToString(); } else { // Session doesn’t exist, set it to the default company_path = “/reflex/SMD”; } 那是因为Session名称“company_path”不存在,但我无法检测到它!