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

 Mikor 发布于 2022-12-07 19:53

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

这是我的代码(与==a 比较的是字符串或基本类型):

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;

观察窗口中的视图: 我的监视窗口的屏幕截图,返回语句的每个部分都有一行. 除了供应部门之外的所有部分都属实.

比较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: 

juharr.. 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.supplyAmperage会导致三元运算符执行this.commandResults.Equals(i.commandResults)即使this.commandResultsnull.

1 个回答
  • 我认为你所拥有的就等同于此

    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.supplyAmperage会导致三元运算符执行this.commandResults.Equals(i.commandResults)即使this.commandResultsnull.

    2022-12-11 03:11 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有