AS3 -TypeError # 1009 - any easy way to find out * that an object reference is zero?

We all get "TypeError # 1009 Unable to access the property or method of referencing a null object" from time to time - it doesn't matter, but sometimes it's hard to debug.

Flash gives you a call stack (which is the start), but leaves it to you to find out where the null object is - can it be determined exactly which link is causing the error?

Given the following (error prone) function:

function nullObjectReferenceError():void
    {
        var obj:Object;
        var prop:* = obj.nonExistentProperty;
    }

Instead of just a call stack from TypeError, I would like to trace something like: "Unable to access the property or method of referencing a null object in obj.nonExistentProperty " - is that even possible?

+3
source share
3 answers

If you select the "Enable debugging" checkbox in the "Publish to Flash IDE Settings" section, it gives the line number in the code that causes the error.

+4
source

The obvious solution is to stop using such generic error prone code in the first place. You should never use the type "*" and almost never should use the type "Object".

To catch it at run time, you can always say:

if(obj == null)
  throw new Error("null obj passed in!!");

if(obj.nonExistentProperty == null)
  throw new Error("obj doesn't have the prop!! the obj was: "+obj);
+2
source

TypeError , .

, ( ).

. , , .

0

Source: https://habr.com/ru/post/1719242/


All Articles