Copy link to clipboard
Copied
When I use the following code:
addEventListener(Event.ENTER_FRAME, ImportantFunction);
function ImportantFunction(event:Event):void // Main Function
{
//Function in here
}
I get the following error.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
The program still works but the error concerns me. I can call the same function using a button click or object change with no problems.
What am I doing wrong?
Copy link to clipboard
Copied
the code you posted wouldn't trigger that error.
click file>publish settings>swf>and tick 'permit debugging'. retest.
the line number of the incorrect reference will be in the error message allowing you to find that code that is problematic.
Copy link to clipboard
Copied
You don't have that addEventListener() attached to anything. You need to reference to a named instance on the stage. That's what your null object reference is referring to. Here's a reference that should help: AS3: Event Handing
Copy link to clipboard
Copied
i don't see any problem using the implicit 'this'.
Copy link to clipboard
Copied
You're probably right.
Copy link to clipboard
Copied
kglad - You may have a point the error doesn't actually point to that line. It points to an if statement in the function. However the program worked fine before I tried to run the function on frame load and it works fine when I remove the line so whatever the debugger says I'm sure its that line.
robdillion - I tried targeting a object or using ".this" still the same result. All the other calls of the functions (change combo box click on button add text to field etc) work fine its just this one.
Copy link to clipboard
Copied
that means the object mentioned doesn't always (while your enterframe is running) exist. ie, it may have existed when that frame first plays but the playhead may leave that frame and that object no longer exists. meanwhile, you're enterframe loop continues to reference an object that no longer exist.
if you don't understand what's undefined and no longer exists, what's the line of code mentioned in the error message?
Copy link to clipboard
Copied
Kglad, Thanks that makes sense. I understand the problem now. I mistakenly thought ENTER_FRAME would run the code once and the never again but it doesn't so my code runs constantly when the conditions are met (I think thats whats happening)
Is there a Even Listener than will run the code only when the frame opens?
Copy link to clipboard
Copied
yes.
var alreadyExecuted:Boolean;
if(!alreadyExecuted){
alreadyExecuted=true;
runOnceF();
}
function runOnceF():void{
the code you want to run once. or this can be placed within the above if-statement.
}
Copy link to clipboard
Copied
FYI - you can also use Enter_Frame like you did, but just remove it in the function, and it will only run once:
someObject.addEventListener(Event.ENTER_FRAME, runOnce);
function runOnce(e:Event):void
{
someObject.removeEventListener(Event.ENTER_FRAME, runOnce);
trace("runOnce");
}
Copy link to clipboard
Copied
Kglad, Robdillion - Thanks for your assistance
Copy link to clipboard
Copied
you're welcome.
Copy link to clipboard
Copied
You're welcome, but the thanks goes to Ken, not me. He did all the work.