Visual Studio中能告诉我这扔引用一个NullReferenceException? [英] Can Visual Studio tell me which reference threw a NullReferenceException?

查看:244
本文介绍了Visual Studio中能告诉我这扔引用一个NullReferenceException?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写单元测试MVC的Web应用程序,并且我已经得到空引用例外,因为嘲笑向上测试对象只有部分初始化。我知道这行抛出例外,它看起来是这样的:

 收益Supervisor.RegistrationInformation.Registrations 
。任何相关(r =>
r.RegistrationCountry.IsUSAOrCandada()​​及&放大器;!
(DatesWorked.Start.HasValue || r.RegistrationDate&下; = DatesWorked.Start.Value)及&放大器;
(DatesWorked.End.HasValue || r.RegistrationExpirationDate>!= DatesWorked.End.Value)及和放大器;
// ...

有很多在那里引用,它们中的任何可能的问题。但是,的NullReferenceException 本身似乎并没有捕捉到其中提到炸毁了我传递一个lambda的事实提出了另一个挑战:至于我可以告诉大家,我无法通过调试期间拉姆达退一步,看看哪些成员研究为空



有没有什么方法可以让我做一个或两个以下内容:




  • 的Visual Studio告诉我到底是哪参考扔的NullReferenceException

  • 如果做不到这一点,有没有一种方法,使通过lambda表达式的调试步骤(或只是将鼠标悬停在东西可看它们的值),因为它是由评估任何



我觉得必须有一种方法做这些事情,但我似乎无法找到它。我在VS2010 Premium和我有ReSharper的,VS电动工具,并安装了一对夫妇的其他扩展。如果有一个插件,这样做,我会被罚款与



修改



由于埃里克利珀所指出的,这是不可能精确定位NR异常的源代码时已发布配置编译。我只问在调试模式下工作。如果Visual Studio中(或者扩展VS)可以跟踪在调试时的参考,这将回答我的问题的根源



修改2:



第二个问题 - 如何突破,并通过一个lambda步骤 - 已回答了,但我仍想知道是否有追查空引用自动方式

解决方案

有不一般的方式做你想要的,没有。要理解为什么,想想当空引用异常被抛出发生了什么。试想一下,你是编译器,你必须发出代码来处理,以abc.Def.Ghi.Jkl()的调用,其中ABC是一个地方,防守和GHI是引用类型的字段,而京客隆是一种方法。没有IL指令,可以做一些复杂的;你必须打破它。所以,你发出的代码等效计划,一切要简单得多。你散发出的程序片段:

  =的temp1 abc.Def; 
TEMP2 = temp1.Ghi;
temp2.​​Jkl();



假设TEMP2为null,因为GHI为空。这一事实将不会被发现,直到JKL被调用时,此时的引发异常的东西不具有TEMP2初始化方式的知识。的这发生在很​​久以前,在过去和机器代码纳秒没有过去的记忆;空引用不留一张小纸条上写着的地方空出来的,当你说比任何更多的A = B + C,所产生的12号不留一张纸条与它一起,说:我是b和c的总和。


I'm writing unit tests for an MVC web app, and I've been getting null reference exceptions because the mocked-up test objects are only partly initialized. I know which line is throwing the exceptions, and it looks something like this:

return Supervisor.RegistrationInformation.Registrations
    .Any(r =>
        r.RegistrationCountry.IsUSAOrCandada() &&
        (!DatesWorked.Start.HasValue || r.RegistrationDate <= DatesWorked.Start.Value) &&
        (!DatesWorked.End.HasValue || r.RegistrationExpirationDate >= DatesWorked.End.Value) &&
        //...

There are a lot of references in there, and any of them could be the problem. However, NullReferenceException itself doesn't seem to capture which reference blew up. The fact that I'm passing in a lambda presents another challenge: As far as I can tell, I can't step through the lambda during debugging and see which members of r are null.

Is there any way I can do one or both of the following:

  • Have Visual Studio tell me exactly which reference threw the NullReferenceException?
  • Failing that, is there a way to make the debugger step through the lambda expression (or just hover over things to see their values) as it's being evaluated by Any?

I feel like there must be a way to do these things, but I can't seem to find it. I'm on VS2010 Premium, and I have Resharper, VS Power Tools, and a couple other extensions installed. If there's an add-on that does this, I'd be fine with that.

Edit:

As Eric Lippert points out, it's impossible to pinpoint the source of an NR exception when the code has been compiled in Release configuration. I'm only asking about working in debug mode. If Visual Studio (or some extension to VS) can track the source of a reference while debugging, that would answer my question.

Edit 2:

The second question--how to break and step through a lambda--has been answered, but I'd still like to know if there's an automatic way to track down a null reference.

解决方案

There is not in general a way to do what you want, no. To understand why, think about what is happening when a null reference exception is thrown. Imagine that you are the compiler, and you must emit code to process a call to abc.Def.Ghi.Jkl(), where abc is a local, Def and Ghi are fields of reference type, and Jkl is a method. There is no IL instruction that can do something that complicated; you have to break it down. So you emit code for an equivalent program where everything is much simpler. You emit the program fragment:

temp1 = abc.Def;
temp2 = temp1.Ghi;
temp2.Jkl();

Suppose temp2 is null because Ghi was null. That fact will not be discovered until Jkl is invoked, at which point the thing throwing the exception does not have any knowledge of how temp2 was initialized. That happened long ago, a nanosecond in the past and machine code has no memory of the past; the null reference does not keep a little note on it that says where the null came from, any more than when you say "a = b + c", the resulting number twelve does not keep a note along with it that says "I was the sum of b and c".

这篇关于Visual Studio中能告诉我这扔引用一个NullReferenceException?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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