views:

23

answers:

1

Hi guys.. I am building a AS3 only project and got runtime error that said "Cannot access a property or method of a null object reference."

Here is my code:

main.as

       public class videoMain extends Sprite{

         private var videoPlayer:Player;

       public function videoMain (){
         loadPlayer()
         loadProgress();
       }

       private function loadProgress():void{

        //the code below gave me null object error.....     
        var byteLoaded:Number=videoPlayer.videoBytesLoaded; //the problem code
        var byteTotal:Number=videoPlayer.videoBytesTotal;  //the problem code

        var percent:Number=Math.floor(byteLoaded/byteTotal)*100;
        }

        private function loadPlayer():void{

        videoPlayer= new Player();
        videoPlayer.createPlayer();
    }

}

Player.as

public function createPlayer():void{

    _loader = new Loader();
    _loader.contentLoaderInfo.addEventListener(Event.INIT, onLoaderInit);
    _loader.load(new URLRequest(playerType));

    }

public function get videoBytesLoaded():Number{
    return _Player.getVideoBytesLoaded();  //youtube api method
    }

public function get videoBytesTotal():Number{
    return _Player.getVideoBytesTotal;    //youtube api method
    }

 private function onLoaderInit(event:Event):void {
    _Player=_loader.content;
        //only show part of codes....
    }

I appreciate any help....Thanks!!!!!
+1  A: 

_Player is only defined after the Event.INIT has fired so any call before the _Player value is defined will throw an error.

You should, at the minimum , have this:

public function videoMain (){
     loadPlayer()
   }

private function onLoaderInit(event:Event):void {
    _Player=_loader.content;
    //only show part of codes....
    loadProgress();

}

but progress events are not static so really you should have an enterFrame event listener in order to listen to the changing values...

private function onLoaderInit(event:Event):void {
    _Player=_loader.content;
    //only show part of codes....

    addEventListener(Event.ENTER_FRAME , enterFrameListener);
}

private function enterFrameListener(event:Event):void
{
    loadProgress();
    // and here you add some way to remove this event listener when 
    // the video is fully loaded
}
PatrickS
Nice.....Thanks for the reply....!!
Jerry