0

I am making a game in ActionScript 3. I have a Menu Class with a method that renders a Menu. I make an instance of Menu in my Main class, and then call the method. When I debug the application I get a null reference error. This is the code of the menu class:

package
{
import flash.display.MovieClip;

import menucomponents.*;

public class Menu extends MovieClip
{
    public function Menu()
    {
        super();
    }

    public function initMenuComponents():void{
        var arrMenuButtons:Array = new Array();

        var btnPlay:MovieClip = new Play();
        var btnOptions:MovieClip = new Options();
        var btnLikeOnFacebbook:MovieClip = new LikeOnFacebook();
        var btnShareOnFacebook:MovieClip = new ShareOnFacebook()

        arrMenuButtons.push(btnPlay);
        arrMenuButtons.push(btnOptions);
        arrMenuButtons.push(btnLikeOnFacebbook);
        arrMenuButtons.push(btnShareOnFacebook);

        var i:int = 0;

        for each(var item in arrMenuButtons){
            item.x = (stage.stageWidth / 2) - (item.width / 2);
            item.y = 100 + i*50;
            item.buttonMode = true;

            i++;
        }
}
}
}

Thanks in advance.

svdotbe
  • 168
  • 1
  • 3
  • 16
  • Didn't you [just ask this question](http://stackoverflow.com/questions/13363504/as3-error-1009)? – Jason Sturges Nov 13 '12 at 20:34
  • use the debugger to see what's going on, and if you don't have a debugger handy, you can try to at least keep a log – Sam I am says Reinstate Monica Nov 13 '12 at 20:40
  • @SamIam I get the error at the line where I try to add an item to the stage. Does that mean the stage is null or my items are null? Sorry if this is a dumb question, i never learned debugging properly at my college. – svdotbe Nov 13 '12 at 20:50
  • @SylvainVansteelandt and what line is that? – Sam I am says Reinstate Monica Nov 13 '12 at 20:55
  • @SamIam I'm sorry, after posting this I tried to add in the beginning of my for each a statement: stage.addChild(item); But the problem remains, it's always the first line of the for each loop. – svdotbe Nov 13 '12 at 20:57
  • I don't see where `stage` is declared, let-alone initialized, but i don't see any specific reason why `item` would throw the null exception error, so i'd probably assume the problem is with `stage` – Sam I am says Reinstate Monica Nov 13 '12 at 21:00
  • @SamIam but stage is normally automaticly added, it's a flash professional project, you can't even instanciate stage i just tried it... – svdotbe Nov 13 '12 at 21:06
  • when is the `initMenuComponents()` called? Most likely you're calling that function before the instance of your posted class (`Menu`) has been added to the stage. – BadFeelingAboutThis Nov 13 '12 at 21:28
  • @SamIam I mean no. First added to stage. Then method called. package { import flash.display.MovieClip; import flash.display.Stage; public class Main extends MovieClip { public function Main() { super(); var menu:Menu = new Menu(); stage.addChild(menu); menu.initMenuComponents(); } } } – svdotbe Nov 13 '12 at 21:32
  • @SylvainVansteelandt i dont know what is causing the error offhand – Sam I am says Reinstate Monica Nov 13 '12 at 21:33
  • @SylvainVansteelandt - update your question with the code instead of putting it in comments (as it's hard to read). – BadFeelingAboutThis Nov 13 '12 at 22:03
  • In your main class constructor (`public function Main()`), move out the line: `menu.initMenyComponents();` and replace it with: `this.addEventListener(Event.ADDED_TO_STAGE,addedToStage);` then create the addedToStage function and call the `menu.initMenuComponents();` there. this will ensure that stage is ready before that method runs. – BadFeelingAboutThis Nov 13 '12 at 22:04
  • This needs to be closed/deleted. Per OPs commends here and in the answer by LondonDurgs, the problem seems to be very localized and it does not help to the community anyway. – dejjub-AIS Dec 06 '12 at 11:28

1 Answers1

0

Your issue is likely that stage is not populated yet when your for loop runs. Try the following:

public class Main extends MovieClip { 
    public function Main() { 
        super(); 
        var menu:Menu = new Menu(); 

        //it's good practice - as sometimes stage actually isn't populated yet when your main constructor runs - to check, though in FlashPro i've never actually encountered this (flex/flashBuilder I have)
        if(stage){
            addedToStage(null);
        }else{
            //stage isn't ready, lets wait until it is
            this.addEventListener(Event.ADDED_TO_STAGE,addedToStage);
        }
    } 

    private function addedToStage(e:Event):void {
        menu.addEventListener(Event.ADDED_TO_STAGE,menuAdded); //this will ensure that stage is available in the menu instance when menuAdded is called.
        stage.addChild(menu); 
    }

    private function menuAdded(e:Event):void {
        menu.initMenuComponents(); 
    }
}
BadFeelingAboutThis
  • 14,445
  • 2
  • 33
  • 40
  • Already tried that, found this on a other website, didn't work. I finally fixed the problem. Found a wicked fix at a site an magically it worked. Save file as AS2, then back as AS3. Worked. – svdotbe Nov 13 '12 at 22:11
  • Your issue wasn't a programming issue then but a FlashPro issue. I'd be skeptical though of that as a real fix. – BadFeelingAboutThis Nov 13 '12 at 22:13