title: AS3 - Access object from another class
body: <p>I have three classes: a Main, a Player and a Car.</p>
  
  <p>The main creates a Car and a Player.
  The Car needs to trace the player's x position.
  But the code below is not working. I have researched and found that declaring it as static should fix the problem although it is not the best way to do it? </p>
  
  <p>Main:</p>
  
  <pre><code>package { 
  
  import flash.display.MovieClip;
  import flash.events.Event;
  import flash.events.MouseEvent;
  
  public class Main extends MovieClip
  {       
      public static var _player:Player;
      public static var _car:Car;
  
      public function Main() 
      {
          createPlayer();
      }
  
      public function createPlayer():void
      {
  
          _player= new Player();
          _car = new Car();
  
          _player.x = stage.stageWidth / 2;
          _player.y = stage.stageHeight / 2;
  
          stage.addChild(_player);
          stage.addChild(_car);
      }
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>Car:</p>
  
  <pre><code>package { 
  
  import flash.display.MovieClip;
  import Main;
  
  public class Player extends MovieClip
  {
      public function Player()
      {
          trace(Main._car.x);
  
      }
  }
  </code></pre>
  
  <p>}</p>
  
  <p>But this causes an error:
      TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at Player()
      at Main/createPlayer()
      at Main()</p>
  
  <p>I want to know why this is giving me an error and what are the other good ways to do this?</p>
  
  <p>//EDIT: Sorry _hero was my mistake, it is actually _car.
  Thank You Very Much...........</p>