title: AS3 TypeError: Error #1009: Cannot access a property or method of a null object reference when importing external .swf
body: <p>I'm importing an external swf into my main project by using the fallowing code:</p>
  
  <pre><code>var FirstLevelLoader:Loader = new Loader();
  var FirstLevelFile:URLRequest = new URLRequest("Mainlv.swf");
  
  
  //load level one
  startlevelbttn.addEventListener(MouseEvent.CLICK, LoadFirstLevel);
  
  function LoadFirstLevel(e:Event){
  FirstLevelLoader.load(FirstLevelFile);
  addChild(FirstLevelLoader);
  }
  </code></pre>
  
  <p>This same type of code works fine when importing other swfs but for Mainlv it doesn't instead it tells me the fallowing error.</p>
  
  <pre><code>TypeError: Error #1009: Cannot access a property or method of a null object reference.
  at Mainlv/createPlayer()
  at Mainlv()
  </code></pre>
  
  <p>So I suppose the error is in the Mainlv.swf file but I don't see what is wrong and running that file independently produces no errors. Here's the code for the fallowing file.</p>
  
  <pre><code>package  
  {
  import flash.display.MovieClip;
  import flash.events.Event;
  import flash.events.MouseEvent;
  import flash.utils.Timer;
  import flash.events.TimerEvent;
  
  
  
  public class Mainlv extends MovieClip
  
  {
  
  
      // player settings      
      private var _moveSpeedMax:Number = 1000;
      private var _rotateSpeedMax:Number = 15;
      private var _decay:Number = .98;
      private var _destinationX:int = 150;
      private var _destinationY:int = 150;
  
      // player
      private var _player:MovieClip;
  
      // global
      private var _isActive:Boolean = false;
  
      private var _dx:Number = 0;
      private var _dy:Number = 0;
  
      private var _vx:Number = 0;
      private var _vy:Number = 0;
  
      private var _trueRotation:Number = 0;
  
      private var allbullets:Array = new Array();
  
      private var allast:Array = new Array();
  
      private var score:int=0;
  
      /**
       * Constructor
       */
      public function Mainlv() 
      {
          // create player object
          createPlayer();
  
  
          // add listeners
          stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
          stage.addEventListener(MouseEvent.MOUSE_OVER, onMouseUpHandler);
          stage.addEventListener(MouseEvent.MOUSE_OVER, onMouseDownHandler);
          stage.addEventListener(MouseEvent.CLICK, triggerbullet);
  
  
      }
  
  
      private function triggerbullet(event:Event){
          var mybullet:Bullet= new Bullet();
          stage.addChild(mybullet);
          mybullet.x=_player.x;
          mybullet.y=_player.y;
          mybullet.rotation=_player.rotation;
  
          allbullets.push(mybullet);
  
  
      }
  
      /**
       * Creates player
       */
      private function createPlayer():void
      {           
          _player = new Player();
          _player.x = stage.stageWidth / 2;
          _player.y = stage.stageHeight / 1.15;
          stage.addChild(_player);
      }
  
      /**
       * EnterFrame Handlers
       */
      private function enterFrameHandler(event:Event):void
      {
          updatePosition();
          updateRotation();
  
          scorebox.text=String(score);
  
          var t:Number = Math.random();
          if(t&gt;0.98) {
              addasteroid();
          }
  
          for (var a:int = 0;a&lt;allast.length;a++) {
              for (var b:int = 0;b&lt;allbullets.length;b++) {
  
                  if (allbullets[b].hitTestObject(allast[a])) {
                      //removeChild(allbullet[b]);
                      removeChild(allast[a]);
  
                      //score     
                      score+=5;
  
                      if (score==100) {
                          dispatchEvent(new Event("next_level"));
                      }
  
                  }
                  if (allast[a].hitTestObject(_player)) {
                      //removeChild(allbullet[b]);
                      trace("hitplayer");
                      endGame();  
                  }
  
  
              }
  
              allast[a].y += 3;
          }
  
      }//function
  
  
  
      /*END GAME*/
      private function endGame() {
          stage.removeChild(_player); 
          dispatchEvent(new Event("END_GAME")); // call End_game function to exit external swf
      }
  
  
  
      /**
       * Calculate Rotation
       */
      private function updateRotation():void
      {
          // calculate rotation
          _dx = _player.x - _destinationX;
          _dy = _player.y - _destinationY;
  
          // which way to rotate
          var rotateTo:Number = getDegrees(getRadians(_dx, _dy)); 
  
          // keep rotation positive, between 0 and 360 degrees
          if (rotateTo &gt; _player.rotation + 180) rotateTo -= 360;
          if (rotateTo &lt; _player.rotation - 180) rotateTo += 360;
  
          // ease rotation
          _trueRotation = (rotateTo - _player.rotation) / _rotateSpeedMax;
  
          // update rotation
          _player.rotation += _trueRotation;          
      }
  
      /**
       * Calculate Position
       */
      private function updatePosition():void
      {
          // check if mouse is down
          if (_isActive)
          {
              // update destination
              _destinationX = stage.mouseX;
              _destinationY = stage.mouseY;
  
              // update velocity
              _vx += (_destinationX - _player.x) / _moveSpeedMax;
              _vy += (_destinationY - _player.y) / _moveSpeedMax;
          }
          else
          {
              // when mouse is not down, update velocity half of normal speed
              _vx += (_destinationX - _player.x) / _moveSpeedMax * .25;
              _vy += (_destinationY - _player.y) / _moveSpeedMax * .25;
          }
  
          // apply decay (drag)
          _vx *= _decay;
          _vy *= _decay;
  
          // if close to target, slow down turn speed
          if (getDistance(_dx, _dy) &lt; 50)
          {
              _trueRotation *= .5;
          }           
  
          // update position
          _player.x += _vx;
          _player.y += _vy;
      }
  
      /**
       * Mouse DOWN handler
       * @param   e
       */
      private function onMouseDownHandler(e:MouseEvent):void 
      {
          _isActive = true;
      }
  
      /**
       * Mouse UP handler
       * @param   e
       */
      private function onMouseUpHandler(e:MouseEvent):void 
      {
          _isActive = false;
      }
  
      /**
       * Get distance
       * @param   delta_x
       * @param   delta_y
       * @return
       */
      public function getDistance(delta_x:Number, delta_y:Number):Number
      {
          return Math.sqrt((delta_x*delta_x)+(delta_y*delta_y));
      }
  
      /**
       * Get radians
       * @param   delta_x
       * @param   delta_y
       * @return
       */
      public function getRadians(delta_x:Number, delta_y:Number):Number
      {
          var r:Number = Math.atan2(delta_y, delta_x);
  
          if (delta_y &lt; 0)
          {
              r += (2 * Math.PI);
          }
          return r;
      }
  
      /**
       * Get degrees
       * @param   radians
       * @return
       */
      public function getDegrees(radians:Number):Number
      {
          return Math.floor(radians/(Math.PI/180));
      }
  
  
      public function addasteroid() {
          var newast:asteroid = new asteroid();
          newast.x=Math.random()*950;
          newast.y=Math.random()*200;
  
          addChild(newast);
  
          allast.push(newast);
      }
  
  
  
  
  
  } //don't code outside of here
  </code></pre>
  
  <p>}//end</p>