title: AS3 - Error #1009: Cannot access a property or method of a null object reference
body: <p>I'm getting the following error when trying to run a short piece of code I'm making for use of a particle effect library.</p>
  
  <blockquote>
    <p>Error #1009: Cannot access a property or method of a null object reference </p>
  </blockquote>
  
  <p>I originally had my entire code running from Main and it worked fine, when I decided to put it into a "ParticleManager" class I begun getting this error. </p>
  
  <p>The error itself happens here - </p>
  
  <pre><code>stage.addEventListener(MouseEvent.CLICK, _onStageMouseDown);
  
      function _onStageMouseDown(e:MouseEvent):void
      {
          //Draws an explosion on to the screen at the position of the mouse click.
          var emitter:Emitter3D = new ParticleExplosion(new Vector3D(mouseX- width/2, mouseY- height/2, 0));
          renderer.addEmitter( emitter );
          emitter.start();
          trace(e);
          trace(renderer.mouseX)
          trace(renderer.mouseY)
      }
  
  }
  </code></pre>
  
  <p>It occurs on the first line there. And I've tried to research it and found this occurs because the stage is not set to anything, but how do I get around that? Why did it work fine running from Main before hand?</p>
  
  <p>Thanks!</p>
  
  <p>Edit - Additional information requested.</p>
  
  <p>The Main class</p>
  
  <pre><code>package 
  {
    import flash.display.Sprite;
  
  [SWF(width='800', height='600', frameRate='60', backgroundColor='#000000')] 
  
      /**
       * ...
       * @author 
       */
  public class Main extends Sprite 
      {
          private var pManager:ParticleManager;
  
  public function Main()
      {
          pManager = new ParticleManager;
  
      }
    }
  }
  </code></pre>
  
  <p>The ParticleManager class, which before was identical just named "Main" and would run from that.</p>
  
  <pre><code>package  
  {
  //Flint imports
    import flash.events.Event;
    import flash.events.MouseEvent;
    import org.flintparticles.common.emitters.Emitter;
    import org.flintparticles.common.actions.ScaleImage;
    import org.flintparticles.common.events.EmitterEvent;
    import org.flintparticles.common.events.ParticleEvent;
    import org.flintparticles.threeD.emitters.Emitter3D;
    import org.flintparticles.threeD.particles.Particle3D;
    import org.flintparticles.threeD.renderers.BitmapRenderer;
    import org.flintparticles.threeD.renderers.controllers.FirstPersonCamera;
    import org.flintparticles.threeD.zones.LineZone;
    //Flash imports
    import flash.display.Sprite;
    import flash.filters.BlurFilter;
    import flash.filters.ColorMatrixFilter;
    import flash.geom.Rectangle;
    import flash.geom.Vector3D;
  
  
  
      /**
       * ...
       * @author 
       */
      public class ParticleManager extends Sprite 
      {
          private var orbitter:FirstPersonCamera;
          private var renderer:BitmapRenderer;
  
  
      public function ParticleManager()
      {
  
      //Sets up the "BitMapRenderer"  a rectangle where the particle effects will be drawn on to
        renderer = new BitmapRenderer( new Rectangle( 0, 0, 800, 600 ), false );
        renderer.x = 0;
        renderer.y = 0;
        renderer.addFilter( new BlurFilter( 2, 2, 1 ) );
        renderer.addFilter( new ColorMatrixFilter( [ 1,0,0,0,0,0,1,0,0,0,0,0,1,0,0,0,0,0,0.95,0 ] ) );
        addChild( renderer );
  
  
        renderer.camera.position = new Vector3D( 0, 0, -400 );
        renderer.camera.target = new Vector3D( 0, 0, 0 );
        renderer.camera.projectionDistance = 400;
  
        //Mouse click
  
          function addedToStageHandler(event:Event):void
          {
              stage.addEventListener(MouseEvent.CLICK, _onStageMouseDown);
          }
  
  
          function _onStageMouseDown(e:MouseEvent):void
          {
              //Draws an explosion on to the screen at the position of the mouse click.
              var emitter:Emitter3D = new ParticleExplosion(new Vector3D(mouseX- width/2, mouseY- height/2, 0));
              renderer.addEmitter( emitter );
              emitter.start();
              trace(e);
              trace(renderer.mouseX)
              trace(renderer.mouseY)
          }
  
      }
  
      public function removeEmitter( ev:EmitterEvent ):void
      {
        Emitter3D( ev.target ).removeEventListener( EmitterEvent.EMITTER_EMPTY, removeEmitter );
        renderer.removeEmitter( Emitter3D( ev.target ) );
      }
  
      public function destroy():void
      {
        for each( var e:Emitter in renderer.emitters )
        {
          e.stop();
        }
      }
    }
  }
  </code></pre>