Home Tutorials Forums Articles Blogs Movies Library Employment Press

Go Back   ActionScript.org Forums > ActionScript Forums Group > ActionScript 3.0

Reply
 
Thread Tools Rating: Thread Rating: 6 votes, 5.00 average. Display Modes
Old 09-13-2007, 08:25 PM   #1
stjarna
Registered User
 
Join Date: Sep 2007
Posts: 3
Red face TypeError: Error #1009: Cannot access a property or method of a null object reference

I'm building an AS3 media player and I am having trouble loading an external movie. I keep getting the error:

TypeError: Error #1009: Cannot access a property or method of a null object reference at CookBookPlayer$iinit()

Here is my script for the main Loader class:

Code:
package  {
	
	
	import flash.display.*;
	import flash.net.URLRequest;
	import flash.events.Event;
	import com.monkeyclaus.mediaplayer.PanControl;
	import com.monkeyclaus.mediaplayer.PlayButton;
	import com.monkeyclaus.mediaplayer.SpectrumGraph;
	import com.monkeyclaus.mediaplayer.VolumeControl;
	import flash.text.TextField;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	import flash.text.TextFormat;
	import flash.utils.Timer;



	public class LoadPlayer extends Sprite{
		
			
			
			private var _loader:Loader;
			
			public function LoadPlayer(){
				
				//Create the loader and add it to the display list
				_loader = new Loader();
				addChild( _loader );
				
				//add the event handler to interact with the movie
				_loader.contentLoaderInfo.addEventListener( Event.INIT, handleInit );
				
				//Load an external movie
				_loader.load (new URLRequest( "MediaPlayer.swf" ));
			}
			
			// Event Handler called when the external loaded movie is ready
			
			private function handleInit (event:Event):void{
				
				var movie:* = _loader.content;
				
				trace("loaded");
			}
			
		}
		
		
}

and here is the code for the media player application I am trying to load:



Code:
package  {
	
	import com.monkeyclaus.mediaplayer.PanControl;
	import com.monkeyclaus.mediaplayer.PlayButton;
	import com.monkeyclaus.mediaplayer.SpectrumGraph;
	import com.monkeyclaus.mediaplayer.VolumeControl;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.text.TextField;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.media.Sound;
	import flash.media.SoundChannel;
	import flash.media.SoundTransform;
	import flash.net.URLRequest;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	public class CookBookPlayer extends Sprite{
		
		private var _channel:SoundChannel;
		private var _displayText:TextField;
		private var _sound:Sound;
		private var _panControl:PanControl;
		private var _playing:Boolean = false;
		private var _playPauseButton:Sprite;
		private var _position:int = 0;
		private var _spectrumGraph:SpectrumGraph;
		private var _volumeControl:VolumeControl;
		
		public function CookBookPlayer() {
			trace("player started");
			//Stage alignment
			stage.scaleMode = flash.display.StageScaleMode.NO_SCALE;
			stage.align = flash.display.StageAlign.TOP_LEFT;
			
			//Enter a frame listener
			var timer:Timer = new Timer(20);
			timer.addEventListener(TimerEvent.TIMER, onTimer);
			timer.start();
			_playing = true;
			
			//Display a text field
			_displayText = new TextField();
			addChild(_displayText);
			_displayText.x = 10;
			_displayText.y = 17;
			_displayText.width = 256;
			_displayText.height = 14;
			
			//Create a sound object
			_sound = new Sound(new URLRequest("ruxpin_turtles_sea_forward.mp3"));
			_sound.addEventListener(Event.ID3, onID3);
			_channel = _sound.play();
			
			//Create a bitmap for spectrum display
			_spectrumGraph = new SpectrumGraph();
			_spectrumGraph.x = 10;
			_spectrumGraph.y = 33;
			addChild(_spectrumGraph);
			
			//Create the play and pause buttons
			_playPauseButton = new PlayButton();
			_playPauseButton.x = 10;
			_playPauseButton.y = 68;
			addChild(_playPauseButton);
			_playPauseButton.addEventListener(MouseEvent.MOUSE_UP, onPlayPause);
			
			
			//Create volume and pan controls
			
			_volumeControl = new VolumeControl();
			_volumeControl.x = 45;
			_volumeControl.y = 68;
			addChild(_volumeControl);
			_volumeControl.addEventListener(Event.CHANGE, onTransform);
			
			_panControl = new PanControl();
			_panControl.x = 164;
			_panControl.y = 68;
			addChild(_panControl);
			_panControl.addEventListener(Event.CHANGE, onTransform);
			
		}
		
		public function onTransform(event:Event):void{
			//Get volume and pan data from controls
			// and apply to a new SoundTransform Object
			_channel.soundTransform = new SoundTransform(_volumeControl.volume, _panControl.pan);
		}
		
		public function onPlayPause(event:MouseEvent):void{
			//If playing, stop and record that position
			if(_playing) {
				_position = _channel.position;
				_channel.stop();
			}
			else{
				//Else, restart at the saved position
				_channel = _sound.play(_position);
			}
			_playing = !_playing;
		}
			
			public function onID3(event:Event):void{
				//Display selected id3 tags in the text field
				_displayText.text = _sound.id3.artist + " : " + _sound.id3.songName;
				_displayText.setTextFormat(new TextFormat("Verdana", 8, 0xffffff));
			}
			
			public function onTimer(event:TimerEvent):void {
				var barWidth:int = 256;
				var barHeight:int = 5;
				
				var loaded:int = _sound.bytesLoaded;
				var total:int = _sound.bytesTotal;
				
				var length:int = _sound.length;
				var position:int = _channel.position;
				
				
				//Draw a background bar
				graphics.clear();
				graphics.beginFill(0xffffff);
				graphics.drawRect(10, 10, barWidth, barHeight);
				graphics.endFill();
				
				if(total > 0) {
					
				//The percent of the sound that has loaded
				var percentBuffered:Number = loaded / total;
				
				//Draw a bar that represents the percent of
				// the sound that has loaded
				graphics.beginFill(0xcccccc);
				graphics.drawRect(10, 10, barWidth * percentBuffered, barHeight);
				graphics.endFill();
				
				//Correct the sound length calculation
				length /= percentBuffered;
				
				//The percent of the sound that has played
				var percentPlayed:Number = position / length;
				
				//Draw a bar that represents the percent of
				// the sound that has played
				
				graphics.beginFill(0xffffff);
				graphics.drawRect(10, 10, barWidth * percentPlayed, barHeight);
				graphics.endFill();
				
				_spectrumGraph.update();
				}
				
				
			}
	}
}

Any ideas? I would greatly appreciate it.
stjarna is offline   Reply With Quote
Old 09-13-2007, 08:26 PM   #2
stjarna
Registered User
 
Join Date: Sep 2007
Posts: 3
Default

by the way, this is an application I am building in FLASH CS3, not FLEX 2
stjarna is offline   Reply With Quote
Old 09-13-2007, 08:54 PM   #3
noflashlight
Registered User
 
Join Date: Aug 2007
Location: New York, NY
Posts: 69
Send a message via MSN to noflashlight
Default

what does your .fla file look like.
my guess is that...
Code:
stage.scaleMode = flash.display.StageScaleMode.NO_SCALE;
stage.align = flash.display.StageAlign.TOP_LEFT;
...is what's doing it.
The stage probably hasn't been instantiated yet.
Try commenting out those two lines and see if the error is still thrown.
noflashlight is offline   Reply With Quote
Old 09-14-2007, 05:07 PM   #4
stjarna
Registered User
 
Join Date: Sep 2007
Posts: 3
Default

Quote:
Originally Posted by noflashlight View Post
what does your .fla file look like.
my guess is that...
Code:
stage.scaleMode = flash.display.StageScaleMode.NO_SCALE;
stage.align = flash.display.StageAlign.TOP_LEFT;
...is what's doing it.
The stage probably hasn't been instantiated yet.
Try commenting out those two lines and see if the error is still thrown.
Thanks for checking out my code.

I commented out those lines and I'm still getting the same error.

I have a blank main parent .swf with the LoadPlayer class where I am trying to load in the the media player movie. The media player movie's class is called "CookBookPlayer". It works fine when ran on it's own. It only seems to be having problems when I try to load it into a parent .swf.
stjarna is offline   Reply With Quote
Old 09-25-2007, 05:24 AM   #5
SAVAGER
Registered User
 
Join Date: Sep 2005
Posts: 6
Default

I am having the same exact problem. Does anyone know why this is?
SAVAGER is offline   Reply With Quote
Old 10-02-2007, 02:04 PM   #6
fenris23
Registered User
 
Join Date: Oct 2007
Posts: 6
Default

I solved this by putting a blank frame in the beggining of my loaded swf, but i dont think this is the best way to do it, anyone has a better sugestion?
fenris23 is offline   Reply With Quote
Old 10-02-2007, 05:25 PM   #7
Slowburn
[Invent:Design:Create]
 
Join Date: Sep 2002
Location: Toronto, ON
Posts: 2,068
Default

In your CookBookPlayer constructor your making reference to the stage possibly before the CookBookPlayer gets added to the displayList.
stage and root do not exist in a DisplayObject until you've added them to the displayList using addChild();

you could set up a listener and wait until the DisplayObject has been added to the stage.

ActionScript Code:
package {      import flash.display.*;      public class Test extends Sprite      {           public function Test():void           {                addEventListener( Event.ADDED, onAdded );           }           public function onAdded( event:Event ):void           {                removeListener( Event.ADDED, onAdded );                stage.scaleMode = StageScaleMode.NO_SCALE;                stage.align = StageAlign.TOP_LEFT;           }      } }

Another idea might be to use your class as a DocumentClass instead of instantiating it ( if your not already )
__________________
o Please do a search before you post
o Please use the [ as][ /as] tag for your code (so we can read it clearly)
Slowburn is offline   Reply With Quote
Old 02-06-2009, 03:51 AM   #8
hitechv1
Registered User
 
Join Date: Feb 2009
Posts: 1
Unhappy TypeError: Error #1009: Cannot access a property or method of a null object reference

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Map_fla::MainTimeline/frame1()


Here is the code for the preloader

Code:
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, loop);
l.contentLoaderInfo.addEventListener(Event.COMPLETE, done);
l.load(new URLRequest('Map.swf'));

function loop(e:ProgressEvent):void {
	var perc:Number = e.bytesLoaded / e.bytesTotal;
	percent.text = Math.ceil(perc*100).toString();
}

function done(e:Event):void {
	removeChildAt(0);
	percent = null;
	addChild(l);
}
here is the code for the Map.swf

Code:
stop();
// This Script is for moving the Map with your Arrow Keys.
stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyPressed);

function KeyPressed(evt:KeyboardEvent):void {
switch (evt.keyCode) {
case Keyboard.UP :
Map_mc.y += 25;
break;
case Keyboard.DOWN :
Map_mc.y -= 25;
break;
case Keyboard.LEFT :
Map_mc.x += 25;
break;
case Keyboard.RIGHT :
Map_mc.x -= 25;
break;
}
}
I'm lost and new to flash I'm thinking it's talking about something in the Map.swf but I don't know what. thanks in advanced
hitechv1 is offline   Reply With Quote
Old 06-17-2009, 02:02 AM   #9
jvrphoto
Registered User
 
Join Date: Jun 2009
Posts: 1
Default

Quote:
Originally Posted by fenris23 View Post
I solved this by putting a blank frame in the beggining of my loaded swf, but i dont think this is the best way to do it, anyone has a better sugestion?
Oh my god thanks! that was so easy!
jvrphoto is offline   Reply With Quote
Old 12-30-2009, 09:29 AM   #10
newbie007
Registered User
 
Join Date: Dec 2009
Posts: 16
Default

my guess is that eventlisteners shuld be removed.. i solved the same error by doing that..
newbie007 is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
child object in timeline access method of parent class pixelmana ActionScript 3.0 2 08-28-2007 08:55 AM
Null Object Reference DanielDraper ActionScript 3.0 0 08-27-2007 01:35 AM
Error #1009: Cannot access a property or method of a null object reference blu3 Flex 2, 3 & 4 9 07-27-2007 10:38 AM
Classes and null object reference doemsche ActionScript 3.0 2 07-24-2007 04:02 PM
trouble with registerClass method of Application object bradmicleah Flash Media Server 1 04-08-2003 11:48 PM


All times are GMT. The time now is 05:52 PM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.