简体   繁体   English

Unity3D 调试神奇地移除 NullReferenceException?

[英]Unity3D debug magically removes NullReferenceException?

A friend of mine, which in a programmer too, has got stuck in some really weird stuff... I can't really explain it, because this code:我的一个朋友,也是程序员,陷入了一些非常奇怪的事情......我无法真正解释它,因为这段代码:

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);
}

Returns退货

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

But if you uncomment the debug lines, it works.但是如果你取消对调试行的注释,它就可以工作。 Can anyone clarify this, please?任何人都可以澄清这一点吗? Thanks for your help!谢谢你的帮助!

It looks like your code is getting a null reference exception , by using the try-catch statement your telling your program to ignore the error and continue running.看起来您的代码正在获取null引用异常,通过使用try-catch语句告诉您的程序忽略错误并继续运行。

The catch statement catches the error and allows you to handle the error however you see fit. catch语句捕获错误并允许您以任何您认为合适的方式处理错误。 In your case, your error handling is just writing the error to the log.在您的情况下,您的错误处理只是将错误写入日志。

By commenting out the Debug statement your not handling your error at all.通过注释掉 Debug 语句,您根本没有处理您的错误。 Your essentially completely ignoring that block of code if and when an error occurs.如果发生错误,您基本上会完全忽略该代码块。 Sometimes the program can "appear" to still run properly if that block of code doesn't have any dependencies.有时,如果该代码块没有任何依赖项,该程序可以“看起来”仍然正常运行。

However you should track down your null reference exception and resolve it or face the wrath of unintended consequences.但是,您应该追踪您的null引用异常并解决它,否则将面临意外后果的愤怒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM