Log in

View Full Version : OOP Error



punstc
06-08-2009, 05:00 PM
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:



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:



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



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.

Medyman
06-09-2009, 01:29 PM
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.

punstc
06-09-2009, 06:02 PM
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