AS3 -TypeError #1009 - 有什么简单的方法可以找出*哪个*对象引用为空吗?
我们时不时都会遇到“TypeError #1009 无法访问空对象引用的属性或方法” - 没什么大不了的,但有时调试起来令人沮丧。
Flash 为您提供了调用堆栈(这是一个开始),但让您自行确定空对象在哪里 - 是否有可能准确地找出哪个引用引发了错误?
给定以下(容易出错)函数:
function nullObjectReferenceError():void
{
var obj:Object;
var prop:* = obj.nonExistentProperty;
}
我想跟踪类似以下内容的不仅仅是 TypeError 中的调用堆栈:“无法访问 obj.nonExistentProperty 处的空对象引用的属性或方法” ” - 这可能吗?
We all get "TypeError #1009 Cannot access a property or method of a null object reference" now and then - no big deal, but sometimes frustrating to debug.
Flash gives you the call stack (which is a start), but leaves it up to you to figure out where the null object is - is it possible to find out exactly which reference is throwing the error?
Given the following (error prone) function:
function nullObjectReferenceError():void
{
var obj:Object;
var prop:* = obj.nonExistentProperty;
}
Rather than just the call stack from the TypeError, I'd like to trace something like: "Cannot access a property or method of a null object reference at obj.nonExistentProperty" - Is this even possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您在 Flash IDE 中的“发布设置”下选中“允许调试”,它会为您提供代码中导致错误的行号。
If you check Permit Debugging under Publish Settings in the Flash IDE, it gives you the line number in your code causing the error.
显而易见的解决方案是首先停止使用这种容易出错的通用代码。您永远不应该使用“*”类型,并且几乎永远不应该使用“Object”类型。
要在运行时捕获它,你总是可以说:
The obvious solution is to stop using such generic error-prone code in the first place. You should never use '*' type, and almost never should use 'Object' type.
To catch it at runtime, you could always say:
如果您捕获到
TypeError
,它不会为您提供更多信息。据我所知,没有已知的方法可以实现此目的(即哪个对象抛出错误)。
最好的办法是在函数的开头设置一个断点并手动调查变量。这就是我所做的,而且对我来说效果相当好。
TypeError
will not give you any more information if you catch it.As far as I know, there is no known way to achieve this (i.e which object threw the Error).
Your best bet will be to set a breakpoint in the beginning of the function and investigate the variables manually. That's what I do, and that works fairly well for me.