簡體   English   中英

有沒有更好的方法來測試NullReferenceException?

[英]Is there a better way to test for a NullReferenceException?

該應用程序正在運行,並且該應用程序的窗口在屏幕上可見,但是已編碼的ui測試始終獲取NullReferenceException:

  • 抽樣=應用
  • sample.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.

如果sampling.Window本身不為 null ,但諸如sampling.Window.Name == null會導致NullReferenceException ,我懷疑該異常來自屬性Nameget訪問器內部。

您是否在沒有優化的情況下(在“調試”模式下)編譯了此文件,並嘗試使用附加的Visual Studio調試器運行它? 當異常發生時,它應該停止執行,並且您應該能夠看到哪個引用為null 它可能位於Visual Studio用顏色突出顯示的行的上方。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM