NitinDixit
NitinDixit

Reputation: 13

Loader is returning a null object in as3

I am trying to make a menu that calls another swf file when the button is clicked. however, the message returned is-

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

The GameController() given above is the document class of the called "HotAirRises.swf"

the code is given below:

package  {

import flash.display.MovieClip;
import fl.controls.Button;
import flash.events.MouseEvent;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.Event;

public class MainController extends MovieClip {


    public function MainController() 
    {
        tester.addEventListener(MouseEvent.CLICK, testIt);
        // constructor code
    }
    private function testIt(e:MouseEvent)
    {
        trace("testing");
        var myLoader:Loader = new Loader();                    
        var url:URLRequest = new URLRequest("HotAirRises.swf"); 
        trace(myLoader,url);
        myLoader.load(url,null);
        addChild(myLoader);
    }
}

}

What am i missing here? something to do with the other HotAirRises.swf file? Please Help!

Upvotes: 0

Views: 112

Answers (1)

George Profenza
George Profenza

Reputation: 51837

My guess is GameController in HotAirRises.swf accesses the stage property(probably to position display objects based on stage dimensions). The problem is that the swf is being loaded, so initially the stage property will be null.

You can try to add the loader straight away, before loading:

var myLoader:Loader = addChild(new Loader()) as Loader; 

(and you wouldn't need addChild(myLoader);) after loading, but this might not work.

To be 100% sure you'll get the expected behaviour (and as a best practice), initialise your GameController on the ADDED_TO_STAGE event handler.

Where your code could be something like:

GameController(){//constructor
   init();
}

private function init():void{
  //initalise Game Controller code
}

it should be something like:

GameController(){//constructor
   addEventListener(Event.ADDED_TO_STAGE, init);
}
private void init():void{
//stage should not be null here, carry on initialising
}

Have a look at this article as well.

Upvotes: 1

Related Questions