2012-04-22 29 views
1

礦,這在程序員也已經陷入了一些很奇怪的東西的一個朋友......我真的不能解釋它,因爲這個代碼:Unity3D調試魔術般地移除NullReferenceException?

try { 
    result = chunks.Find (delegate(float[] fa2) { 
//           Debug.Log("fa2: " + fa2); 
//           Debug.Log("fa: " + fa); 
     return fa2[0] == fa[0] && fa2[1] == fa[1] && fa2[2] == fa[2]; 
    }); 
} 
catch (Exception e) { 
    Debug.LogError("result1: " + e); 
} 

返回

result1: System.NullReferenceException: Object reference not set to an instance of an object 
at March+<BuildLand>c__AnonStorey0.<>m__3 (System.Single[] fa2) [0x00009] in C:\Users\Roy\Documents\March\Assets\Scripts\March.cs:262 
at System.Collections.Generic.List`1[System.Single[]].GetIndex (Int32 startIndex, Int32 count, System.Predicate`1 match) [0x00000] in <filename unknown>:0 
at System.Collections.Generic.List`1[System.Single[]].Find (System.Predicate`1 match) [0x00000] in <filename unknown>:0 
at March.BuildLand() [0x0012c] in C:\Users\Roy\Documents\March\Assets\Scripts\March.cs:257 

但是,如果您取消註釋調試行,它的工作原理。 任何人都可以澄清這一點嗎?謝謝你的幫助!

+0

您是否在www.answers.unity3d.com上提問過此問題?那裏可能更多關注。 – 2012-04-22 15:12:46

+0

我想我會那麼做,我認爲將它放在SO中可能會更容易,因爲它在C#中,Unity與此無關。 – 2012-04-22 17:53:31

+0

我知道與團結的問題,我仍然恨這一統一。什麼是「chunks.Find」? – 2013-04-03 23:56:20

回答

2

它看起來像你的代碼獲取空引用異常,通過使用try-catch語句告訴你的程序忽略錯誤並繼續運行。

catch語句捕獲錯誤並允許您處理錯誤,但您認爲合適。在你的情況下,你的錯誤處理只是將錯誤寫入日誌。通過評論Debug聲明你根本不處理你的錯誤。當發生錯誤時,您基本上完全忽略了該代碼塊。有時如果該代碼塊沒有任何依賴關係,程序可以「出現」以仍然正常運行。但是,你應該追蹤你的空引用異常並解決它或面對意想不到的後果的憤怒。