开发者

AS3: Null object reference when attempting to check if it exists?

I have a weird problem. An Object is being passed to my function, and some parameters are optional, so naturally I would check to see if they are there, and if not, do nothing.

However, I'm getting a null reference error (#1009) when I'm just checking it. Here's the sample:

public function parseObject(params:Object) {
   if (params.optionalParam)
       开发者_开发百科trace("Got Optional Parameter!");
}

The error is returned on the line with the if statement. Changing it to check for null (if (params.optionalParam == null)) doesn't work either. The players seems to just give up if an object doesn't exist.

Is there any logical reason for this to happen? Or is it some weird bug that has just surfaced?

Thanks in Advance, -Esa


If your params object is null, then you will get a null reference error when trying to access its' optionalParam property.

Try something like:

if (params == null)
{ trace("params is null!"); }
else if (params.optionalParam != null)
{ trace("Got optional parameter!"); }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜