Skip to main content
Mr. Baker the Shoe Maker
Inspiring
February 6, 2015
Question

TypeError: Error #1009 - I know how to fix it. But I don't know whats going on!

  • February 6, 2015
  • 1 reply
  • 834 views

I have a project that I’m working on that includes working in different scenes and frames. It’s a book project with lots of pages (frames) and chapters (scenes) so I have can’t avoid working in a single frame (like a game). I get a runtime error (TypeError: Error #1009: Cannot access a property or method of a null object reference.) when accessing an object inside of a movieclip. However, I have identified the problem and know how to fix it. But when I fix it the functionality goes away. Here’s how it happens:

  • On frame #1 of Scene 2 I click on a button to go to a frame #2.
  • On frame #2 is the offending code:

     stage.addEventListener(MouseEvent.MOUSE_UP, moveChapters);

//********************Navigation Bar code ********************//

function moveChapters(event:MouseEvent):void

{

        if(event.target == bottomNav.Chapter_1)

        {

        trace("Chapter #1 is working!");

        }

}

  • FYI, this code is part of a navigation bar the will take the user to different chapters. It is visible on stage. I haven't fully built it yet. For example, "if( event.target == bottomNav.Chapter_1)" will take the user to chapter 1, "bottomNav.Chapter_2," will take the user to chapter 2, so on and so forth. "bottomNav" is a movieclip and "Chapter_1" is a button inside it.
  • When I go back to frame #1 of Scene 2 and then click on “Home” button to go back to the main page (Frame #1 of Scene #2) I get the Error #1009.
  • When I comment out theif(event.target == bottomNav.Chapter_1)” I don’t get the error but know I don’t have any functionality.

Is there a better way to do this?

This topic has been closed for replies.

1 reply

robdillon
Participating Frequently
February 6, 2015

Adding the event listener to the stage for this sort of functionality is a bad idea. Even though Flash can, apparently, resolve the objects inside the movieClip, you are making the connection between the object and the event that you want to capture more difficult than it should be. A better approach is to place all of the target objects into an array and then attach the event listener to each object in the array. Then you can easily extract the target from the event.

Something like this might work for you:

-------

var chapterList:Array = new Array(bottomNav.Chapter_1,bottomNav.Chapter_2,bottomNav.Chapter_3);

for(var i in chapterList) {

     chapterList.addEventListener(MouseEvent.MOUSE_UP,moveChapters);

}

function moveChapters(event:MouseEvent):void {

     var thisChapter:Object = event.target;

    

           //  etc

}

Mr. Baker the Shoe Maker
Inspiring
February 7, 2015

Thanks. I will try this approach.