2014-12-03 20 views
4

我重寫了我的一個類的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的比較是唯一這可能會導致一個空的異常,正如你從代碼中看到的,這個場景應該由三元運算符來處理。不僅如此,而且在它失敗的情況下,它不應該到達該部分,因爲該行應該已經停止在第一個假部分上執行。這怎麼可能發生?

編輯: 如這裏要求是異常的詳細信息(注意,這已經由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

您能否創建一個演示錯誤的簡單示例? – 2014-12-03 16:48:59

+0

此外,有點偏移,你的代碼可以通過用'object.Equals(this.commandResults,i.commandResults)'替換三元語句來改進。 – 2014-12-03 16:50:17

+0

異常說的是什麼? – 2014-12-03 16:50:38

回答

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

+0

你也可以在第一個例子中添加圓括號':'結束。 – 2014-12-03 17:05:15

+0

此行爲的參考是http://msdn.microsoft.com/en-us/library/2bxt6kc4.aspx – 2014-12-03 17:06:27

相關問題