Skip to main content
Participant
December 7, 2014
Answered

Error #1009: When eventlistener is other than frame 1

  • December 7, 2014
  • 1 reply
  • 1467 views

So, what I'm trying to do is build a simple page where from the first page with buttons. You click a button and it fires off a simple gotoandstop function like this:


stop();

l2v2_btn.addEventListener(MouseEvent.CLICK, l2v2);

function l2v2(event:MouseEvent):void

{

gotoAndStop(2);

}

From there I want each page to have a bunch of buttons, each button has a sound that plays when you hit it. Wonder, works, easy, swell.

The problem arises when I try to attach a function to those buttons. I want each button press to add a child, or remove a child. Now...when I just do it all on frame 1 - no problem, but when I go from frame 1 (main menu to any other frame (at this point frame 2) I get this, an error that I'm so sick of I can puke.

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

at shit_fla::MainTimeline/frame2()

at flash.display::MovieClip/gotoAndStop()

at shit_fla::MainTimeline/l2v2()

actions on frame 1 are printed above

code on frame 2 ate here:


var nd:And = new And();

var big:Big = new Big();

var current = "none";

and_btn.addEventListener(MouseEvent.CLICK, fAnd);

big_btn.addEventListener(MouseEvent.CLICK, fBig);

function fBig(event:MouseEvent):void

{

if(current == "none")

{

addChild(nd);

current = nd.name;

}

else if(current != "none")

{

removeChild(getChildByName(current));

addChild(nd);

nd.x = 818;

nd.y = 438;

current = nd.name;

}

}

function fAnd(event:MouseEvent):void

{

if(current == "none")

{

addChild(big);

current = big.name;

}

else if(current != "none")

{

removeChild(getChildByName(current));

addChild(big);

current = big.name;

}

}

Basically I just want a main page, I click a button takes me to another page with buttons, each button plays a vocabulary audio and adds a sprite (vocab word to the stage) when

It works when I'm on frame one, the swapping out the sprites, but when I try to go another frame with the same it get the aforementioned error. I know this is a simple fix, could someone please help? This is driving me bonkers


This topic has been closed for replies.
Correct answer JJangmyeon

start a new fla.

add a keyframe to frame 2.

add something to the stage and convert it to a button or movieclip and assign it an instance name.

open the actions panel and reference that instance name is some actionscript that's in frame 2.

any problem?


Actually this seemed to fix my problem:

this.addEventListener(Event.ENTER_FRAME, onEnterFrame, false);

      //and call this checker function continually until all of the buttons are accounted for

      function onEnterFrame(e:Event):void

      {

            if(and_btn != null && big_btn != null )

            {

                 and_btn.addEventListener(MouseEvent.CLICK, fAnd);

                 big_btn.addEventListener(MouseEvent.CLICK, fBig);

            //clean up the enter frame listener so that this function no longer gets called

            this.removeEventListener(Event.ENTER_FRAME, onEnterFrame, false);

            }

      }

Of course now I don't know how to add sprites to the stage at an initial position other than 0,0.

1 reply

kglad
Community Expert
Community Expert
December 7, 2014

click file>publish settings>and tick 'permit debugging'>retest.

the line number that's trying to reference a non existant object will be in the error message.  if that doesn't solve your problem, copy and paste the problematic line of code in your next message.

p.s.  you might want to read about the 1009 error here, Debugging ActionScript 3.0 Errors

Participant
December 7, 2014

Thanks, I did what you suggested, and still got the same error. I haven't used flash in years (like 10) so rusty is beyond how I am, not to mention I was never adept at it. That said, when I checked the permit=-debugging and re-ran, nothing new happened it still spit out:

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

  at TesT_2_fla::MainTimeline/frame2()[TesT_2_fla.MainTimeline::frame2:6]

This isn't coming up as an error though.

These seem to be the offending lines (since the program runs, doesn't work of course, but runs without flash complaining) when I comment them out

and_btn.addEventListener(MouseEvent.CLICK, fAnd);

big_btn.addEventListener(MouseEvent.CLICK, fBig);

I have nothing on the frame 1 stage, the only code I have is gotoAndStop(2) - I've tried running it without this code and same problem

frame 2 I have the two buttons and the second batch of code above.

The thing is when I have it all on frame run, it runs without a hitch.

What's happening (at least it seems to me) is the play head gets to frame 2, runs the code before the stage is populated and doesn't see the buttons and balks, is there any way around this?

Thanks for your reply and any further help you may be able to offer.

I' suprised I haven't seen anyone else with this problem, it just seems like such a noobie mistake is being made somewhere.

kglad
Community Expert
Community Expert
December 7, 2014

the error is on line 6.

if that references and_btn, it means and_btn doesn't exist when that line of code executes.  likewise, if line 6 references big_btn.