在调试器中观察值时,空引用异常有效

时间:2014-12-03 16:45:03

标签: c# nullreferenceexception

我正在覆盖我的一个类上的equals函数并且我得到一个零点异常,尽管当我在" watch"中输入相同的代码时调试器的一部分没有异常。

这是我的代码(与==比较的任何东西都是字符串或基本类型):

return this.workOrder == i.workOrder
    && this.upi == i.upi
    && this.testName == i.testName
    && BasicFunctions.ArraysEqual(this.testTrays, i.testTrays)
    && this.supplyVoltage == i.supplyVoltage
    && this.supplyAmperage == i.supplyAmperage
    && this.commandResults == null ? i.commandResults == null : this.commandResults.Equals(i.commandResults)
    && this.id == i.id;

观察窗口中的视图: Screenshot of my watch window with one line for each section of the return statement. All but the supplyAmperage section are true.

commandResults的比较是唯一可能导致null异常的事情,正如您可以从代码中看到的那样,这个场景应该由三元运算符处理。不仅如此,但在失败的情况下,它应该永远不会到达该部分,因为该行应该在第一个假部分停止执行。怎么会发生这种情况?

修改 这里要求的是异常的细节(请注意,这已由ArrayEquals函数调用,异常不在列出的代码中使用的内容)

System.NullReferenceException was unhandled
  Message="Object reference not set to an instance of an object."
  Source="ATE"
  StackTrace:
       at ATE.Network.TestLocationListener.TestClientInformation.Equals(Object obj) in C:\Users\jdudley\git\ATE\ATE\ATE\Network\TestLocationListener.cs:line 85
       at ATE.BasicFunctions.ArraysEqual[T](T[] a1, T[] a2) in C:\Users\jdudley\git\ATE\ATE\ATE\BasicFunctions.cs:line 150
       at ATE_Remote_Controller.Form1.remoteClient1_StatusUpdated(Object sender) in C:\Users\jdudley\git\ATE\ATE\ATE Remote Controller\Form1.cs:line 25
       at ATE.Network.RemoteClient.statusRead(JSONReadCallbackResult res) in C:\Users\jdudley\git\ATE\ATE\ATE\Network\RemoteClient.cs:line 153
       at ATE.Network.JSONReader.Receive(IAsyncResult ar) in C:\Users\jdudley\git\ATE\ATE\ATE\Network\JSONReader.cs:line 236
       at System.Net.LazyAsyncResult.Complete(IntPtr userToken)
       at System.Net.ContextAwareResult.CompleteCallback(Object state)
       at System.Threading.ExecutionContext.runTryCode(Object userData)
       at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
       at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Net.ContextAwareResult.Complete(IntPtr userToken)
       at System.Net.LazyAsyncResult.ProtectedInvokeCallback(Object result, IntPtr userToken)
       at System.Net.Sockets.BaseOverlappedAsyncResult.CompletionPortCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* nativeOverlapped)
       at System.Threading._IOCompletionCallback.PerformIOCompletionCallback(UInt32 errorCode, UInt32 numBytes, NativeOverlapped* pOVERLAP)
  InnerException: 

1 个答案:

答案 0 :(得分:7)

我认为你所拥有的相当于这个

return this.workOrder == (i.workOrder
    && this.upi == i.upi
    && this.testName == i.testName
    && BasicFunctions.ArraysEqual(this.testTrays, i.testTrays)
    && this.supplyVoltage == i.supplyVoltage
    && this.supplyAmperage == i.supplyAmperage
    && this.commandResults == null) ? 
        i.commandResults == null : 
        (this.commandResults.Equals(i.commandResults)
        && this.id == i.id);

当你想要的是这个。

return this.workOrder == i.workOrder
    && this.upi == i.upi
    && this.testName == i.testName
    && BasicFunctions.ArraysEqual(this.testTrays, i.testTrays)
    && this.supplyVoltage == i.supplyVoltage
    && this.supplyAmperage == i.supplyAmperage
    && (this.commandResults == null ? 
        i.commandResults == null : 
        this.commandResults.Equals(i.commandResults))
    && this.id == i.id;

基本上,如果以前的任何陈述都是错误的,那么即使this.supplyAmperage == i.supplyAmperagethis.commandResults.Equals(i.commandResults)this.commandResults也会导致三元运算符执行null