Skip to main content
msa96947redlands
Participating Frequently
January 15, 2018
Answered

Getting TypeError: Error #1009; but wrecking brains on why it is going wrong.

  • January 15, 2018
  • 2 replies
  • 533 views

I am wrecking my brains, I am creating an animation and have some simple AS3 code but I honestly cannot see what I am doing wrong. This is the error I get below:

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

at animationno2V2_fla::MainTimeline/frame1()

I have attached the hyperlink access to the code on my google drive:

animation no2V2.fla - Google Drive

animation no2V2.swf - Google Drive

please help.

Thank you.

This topic has been closed for replies.
Correct answer Robert Mc Dowell

First of all remove all the code on Frame 1 and add the name "Main" into the class field on your FLA propery, then

add this code:

//////////////////////////////

package {

    import flash.display.MovieClip;

    import flash.events.MouseEvent;

    import flash.events.Event;

    import flash.display.SimpleButton;

    public class Main extends MovieClip {

        public var first_btn:SimpleButton;

        public var second_btn:SimpleButton;

        public var third_btn:SimpleButton;

      

        public function Main(){

            addEventListener(Event.ADDED_TO_STAGE,addedToStageHandler);

        }

      

        private function addedToStageHandler(event:Event):void{

            try{

                first_btn.addEventListener(MouseEvent.CLICK,startAnimation);

                second_btn.addEventListener(MouseEvent.CLICK,middleAnimation);

                third_btn.addEventListener(MouseEvent.CLICK,endAnimation);

            }catch(event:*){

                trace("runtime error!");

            }

        }

        private function startAnimation(event:MouseEvent):void{

            gotoAndPlay("start");

        }

      

        private function middleAnimation(event:MouseEvent):void{

            gotoAndPlay("middle");

        }

      

        private function endAnimation(event:MouseEvent):void{

            gotoAndPlay("end");

        }

    }

}

/////////////////////////////////

after copy and paste your 3 buttons of the frame "start"

to the frame 1 and make them invisible. You error come by the fact that

every element of your scene must exist on the first frame if you declare it on the first frame.

that's it

2 replies

Robert Mc Dowell
Robert Mc DowellCorrect answer
Legend
January 15, 2018

First of all remove all the code on Frame 1 and add the name "Main" into the class field on your FLA propery, then

add this code:

//////////////////////////////

package {

    import flash.display.MovieClip;

    import flash.events.MouseEvent;

    import flash.events.Event;

    import flash.display.SimpleButton;

    public class Main extends MovieClip {

        public var first_btn:SimpleButton;

        public var second_btn:SimpleButton;

        public var third_btn:SimpleButton;

      

        public function Main(){

            addEventListener(Event.ADDED_TO_STAGE,addedToStageHandler);

        }

      

        private function addedToStageHandler(event:Event):void{

            try{

                first_btn.addEventListener(MouseEvent.CLICK,startAnimation);

                second_btn.addEventListener(MouseEvent.CLICK,middleAnimation);

                third_btn.addEventListener(MouseEvent.CLICK,endAnimation);

            }catch(event:*){

                trace("runtime error!");

            }

        }

        private function startAnimation(event:MouseEvent):void{

            gotoAndPlay("start");

        }

      

        private function middleAnimation(event:MouseEvent):void{

            gotoAndPlay("middle");

        }

      

        private function endAnimation(event:MouseEvent):void{

            gotoAndPlay("end");

        }

    }

}

/////////////////////////////////

after copy and paste your 3 buttons of the frame "start"

to the frame 1 and make them invisible. You error come by the fact that

every element of your scene must exist on the first frame if you declare it on the first frame.

that's it

msa96947redlands
Participating Frequently
January 15, 2018

Thank you Robert Mc Dowell, I now know what I did wrong, you have been helpful.

I have been meaning to integrate object oriented code and I will do this.

Thank you again.

Robert Mc Dowell
Legend
January 15, 2018

you are welcome.

> I have been meaning to integrate object oriented code and I will do this.

OOP is much more complex, a good habit to separate design with code is a must

to not be in trouble. The compiler is much more accurate when the code is on a file

and can be more explicit when an error occurs.

thanks for your mention

kglad
Community Expert
Community Expert
January 15, 2018

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

the problematic line number that references a non-existent object will be in the error message.