Results 1 to 3 of 3

Thread: OOP Error

  1. #1
    Join Date
    Aug 2007
    Location
    Harrisburg, PA
    Posts
    131
    Thanks
    6
    Thanked 9 Times in 9 Posts

    Default OOP Error

    I'm not sure what I'm missing I can't find anything wrong. Eclipse isn't showing any errors but when I run in Flash I get the following error.

    TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at AS::Page()
    at AS::Background()
    at CoreyDouglas()
    I have 3 classes The Main being CoreyDouglas where I am initiating a new background which extends my page class.

    the 3 classes look like this:

    CoreyDouglas:

    Code:
    package {
    	import AS.Background;
    	
    	import flash.display.Sprite;
    
    	public class CoreyDouglas extends Sprite
    	{
    		private var background:Background;
    		public function CoreyDouglas()
    		{
    			background = new Background("images/background");
    			this.addChildAt(background, 0);
    		}
    	}
    }
    background:

    Code:
    package AS
    {
    	import flash.display.Bitmap;
    	import flash.display.Loader;
    	import flash.events.Event;
    	import flash.net.URLRequest;
    	
    	public class Background extends Page
    	{
    		private var bgsrc:String;
    		private var loader:Loader;
    		private var bg:Bitmap;
    		private var bgWidth:Number;
    		private var bgHeight:Number;
    		
    		public function Background(src:String)
    		{
    			super();
    			bgsrc = src;
    		}
    		
    		override protected function init(e:Event):void
    		{
    			loader = new Loader();
    			loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
    			loader.load(new URLRequest(bgsrc));
    		}
    		
    		private function onLoadComplete(e:Event):void
    		{
    			bg = new Bitmap(e.currentTarget.content);
    			bg.smoothing = true;
    			
    			bgHeight = bg.height / bg.width;
    			bgWidth = bg.width / bg.height;
    			
    			if ((stage.stageHeight / stage.stageWidth) < bgHeight) 
    			{
    				bg.width = stage.stageWidth;
    				bg.height = bgHeight * bg.width;
    			} 
    			else 
    			{
    				bg.height = stage.stageHeight;
    				bg.width = bgWidth * bg.height;
    			}
    			addChild(bg);
    		}
    	}
    }
    and the Page Class

    Code:
    package AS
    {
    	import flash.display.Sprite;
    	import flash.events.Event;
    
    	public class Page extends Sprite
    	{
    		public function Page()
    		{
    			super();
    			this.addEventListener(Event.ADDED_TO_STAGE, init);
    			stage.addEventListener(Event.RESIZE, onStageResize);
    		}
    		
    		protected function init(e:Event):void {};
    		protected function onStageResize(e:Event):void {};
    	}
    }
    This is probably something extremely simple that I can't see. Thanks for your help I appreciate it.

  2. #2
    Join Date
    Mar 2007
    Location
    Currently: New York/Philadelphia
    Posts
    2,735
    Thanks
    3
    Thanked 519 Times in 507 Posts

    Default

    I think the error is with this.addChildAt(background, 0);. If you're trying to add that object to the stage, you might want to add it to the stage directly.

  3. #3
    Join Date
    Aug 2007
    Location
    Harrisburg, PA
    Posts
    131
    Thanks
    6
    Thanked 9 Times in 9 Posts

    Default

    I found the problem late last night.

    It was trying to add The resize event listener on to the stage before it was actually added onto the stage. I moved my addEventListener for the stage resize into the function that listened for ADD_TO_STAGE and then it worked fine.

    It was one of those very frustrating problems with an easy solution.

    Thanks for looking I appreciate it

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •