Skip to main content
Inspiring
December 8, 2011
Answered

Error #1009: Cannot access a property or method of a null object reference.

  • December 8, 2011
  • 1 reply
  • 1110 views

Hi guys, was wondering if you could shed some light on a problem I am having with AS3!

In frame 1 I have an actions keyframe with my listener and function for a button.

In frame 2 I have an instance of a button.

Frame 3 doesn't have the button.

In frame 4 I have an instance of the button again.

Because the button doesn't exist in the first frame I get the Error #1009.

I move the button from frame 2 to frame 1 and it works, the error stops popping up, yet the button on frame 4 does not work. (I don't even want the button to be on frame 1!)

Is there anyway for the button to be "recognised" by the code in frame 1 without having to have an instance of it so the button can call the function no matter what frame it is in?

I hope this makes sense to somebody because it's driving me nuts

Cheers in advance!

This topic has been closed for replies.
Correct answer Ned Murphy

The button needs to exist where the code is that is trying to target it.  What you should consider doing is just have one isntance of the button that extnds thru all the frames and control its visibility so that it is not visible where you don't want to see it and is visible where you do.

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
December 8, 2011

The button needs to exist where the code is that is trying to target it.  What you should consider doing is just have one isntance of the button that extnds thru all the frames and control its visibility so that it is not visible where you don't want to see it and is visible where you do.

RobulonZAuthor
Inspiring
December 8, 2011

Cheers Ned,

I've had a go at doing what you have suggested and it seems to be the best way of getting around my problem!

If only the code could look in the library or something like that to know that the button exists..meh I'm still an as3 noob!

Cheers again!

Ned Murphy
Legend
December 8, 2011

What you have in the library means next to nothing to the workinbg end of your file.  The button only exists on the stage, and where code is being assigned to it, it must exist.

Another option would be to assign your event listeners in each frame where the button instance pops up.  The event handler function does not need to be repeated since it is not trying to target the button... just the listener code does that.