【问题标题】:Is there a better way to test for a NullReferenceException?有没有更好的方法来测试 NullReferenceException?
【发布时间】:2014-08-31 23:18:39
【问题描述】:

应用程序正在运行并且应用程序的窗口在屏幕上可见, 但是编码的 ui 测试总是得到 NullReferenceException:

  • 采样 = 应用程序
  • sampling.Window = 应用程序的主窗口

代码:

if(object.ReferenceEquals(sampling, null)) // sampling is not a null reference (debug output)
if(sampling == null) //  sampling is not null
if(object.ReferenceEquals(sampling.Window, null)) //  sampling.Window is not a null reference
if(sampling.Window == null) //  sampling.Window is not null
if (sampling.Window.Exists) //  sampling.Window exists? True
if(sampling.Window.TryGetClickablePoint(out pt)) //  Got clickable point? False  Point = {X=0,Y=0}

if(object.ReferenceEquals(sampling.Window.BoundingRectangle, null)) //  Exception: object reference not set to an instance of an object.
if(object.ReferenceEquals(sampling.Window.ControlType, null)) //  Exception: object reference not set to an instance of an object.
if(object.ReferenceEquals(sampling.Window.Name, null)) //  Exception: object reference not set to an instance of an object.
if(object.ReferenceEquals(sampling.Window.ClassName, null)) //  Exception: object reference not set to an instance of an object.
if(sampling.Window.BoundingRectangle == null) //   Exception: object reference not set to an instance of an object.
if(sampling.Window.ControlType == null) //   Exception: object reference not set to an instance of an object.
if(sampling.Window.Name == null) //   Exception: object reference not set to an instance of an object.
if(sampling.Window.ClassName == null) //   Exception: object reference not set to an instance of an object.

【问题讨论】:

  • 您需要更好地格式化您的问题。代码应该被格式化,异常堆栈应该是块引号。您还应该用您使用的语言标记问题。
  • NullReferenceException 的几乎所有情况都是相同的。请参阅“What is a NullReferenceException in .NET?”获取一些提示。

标签: c# visual-studio-2010 nullreferenceexception coded-ui-tests


【解决方案1】:

如果sampling.Window 本身不是 null,但像sampling.Window.Name == null 这样的东西会导致NullReferenceException,我怀疑异常来自属性@ 的get 访问器内部987654326@.

您是否在没有优化的情况下编译了它(在“调试”模式下)并尝试在附加的 Visual Studio 调试器的情况下运行它?当异常发生时它应该停止执行,并且您应该能够看到哪个引用是null。它可能位于 Visual Studio 用颜色突出显示的行的正上方。

【讨论】:

  • 是的,我已经在“调试”模式下编译并调试了几次。问题是没有空引用。问题似乎是应用程序的启动: root = ApplicationUnderTest.Launch(Settings.Default.SamplingAppUrl);根(ApplicationUnderTest 对象)的属性,例如HasTitleBar、Maximized 等都会抛出 NullReferenceException。
猜你喜欢
  • 2019-08-26
  • 2013-08-18
  • 1970-01-01
  • 2018-02-15
  • 2012-04-28
  • 2020-05-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多