null引用不评估为空

Max*_*ges 2 vb.net

我试图在我得到它的值之前测试一个对象是否是什么,但是我得到一个错误"NullReferenceException"

发生在这里的第一行:

If Not ORInvoiceLineRet.InvoiceLineRet.ItemRef.FullName Is Nothing Then
    li.FullName = ORInvoiceLineRet.InvoiceLineRet.ItemRef.FullName.GetValue()
End If
Run Code Online (Sandbox Code Playgroud)

System.NullReferenceException {"对象引用未设置为对象的实例."}

如何在不尝试处理try/catch中的错误的情况下测试?

Tim*_*ter 7

涉及的对象越多越好:

  • ORInvoiceLineRet 什么都不是
  • ORInvoiceLineRet.InvoiceLineRet 什么都不是
  • ORInvoiceLineRet.InvoiceLineRet.ItemRef 什么都不是
  • ORInvoiceLineRet.InvoiceLineRet.ItemRef.FullName 什么都不是

所以这里唯一安全的方法是:

If ORInvoiceLineRet IsNot Nothing _
   AndAlso ORInvoiceLineRet.InvoiceLineRet IsNot Nothing _
   AndAlso ORInvoiceLineRet.InvoiceLineRet.ItemRef IsNot Nothing _
   AndAlso ORInvoiceLineRet.InvoiceLineRet.ItemRef.FullName IsNot Nothing  Then
    li.FullName = ORInvoiceLineRet.InvoiceLineRet.ItemRef.FullName.GetValue()
End If
Run Code Online (Sandbox Code Playgroud)