title: TypeError: Error #1009: Cannot access a property or method of a null object reference. Class references maybe?
body: <p>I get this error when I run my code:</p>
  
  <blockquote>
    <p>TypeError: Error #1009: Cannot access a property or method of a null
    object reference. at scripts::Enemy/moveToward()</p>
  </blockquote>
  
  <p>I'm assuming it has something to do with registering the player on the stage. I have 3 classes so far: Engine, Player and Enemy. I'm currently building a side scrolling beat em up game.</p>
  
  <pre><code>package scripts 
  {
      import flash.display.*;
      import flash.events.*;
      import flash.events.Event;
      import flash.display.Stage;
  
      public class Enemy extends MovieClip
      {
          var enemySpeed: Number = 3;
          var xDistance: Number = 50
          var yDistance: Number = 20;
  
          public var player: Player;
  
                  public function Enemy()
                  {
                          this.addEventListener(Event.ADDED_TO_STAGE, addListeners)
                  }
  
          public function addListeners(event: Event):void
          {
              stage.addEventListener(Event.ENTER_FRAME, moveToward);
              trace("handeled");
          }
  
          function moveToward(event: Event):void
          {
              if(this.x &lt; player.x - xDistance)
              {
                  this.x += enemySpeed;
                  this.scaleX = -1;
              }
              if(this(root).x &gt; player.x+xDistance)
              {
                  this.x -= enemySpeed;
                  this. scaleX = 1;
              }
  
              if(this.y &lt; player.y-yDistance)
              {
                  this.y += enemySpeed;
              }
              if(this.y &gt; player.y+yDistance)
              {
                  this.y -= enemySpeed;
              }
          }
  </code></pre>