Yep, I've found this is an infamous error msg and I've actually fixed it the first time it appeared. However, I'm not sure how to deal with it with the new field it's complaining about.
Forgive me, this is my first actionscript where I'm using a main file and its' companion package file. My very first actionscript, well I had all my code in one file and then noticed example code in tutorials were separated in packages. So, I have split off my one file into the convention where the class is in an external file but now, I am encountering the 1009 type error.
In short, I'm trying to add a sprite using an external jpg file and also added in a function so that I can move it. I put all loads and move function in the class package file. The error is that when I try to access the "addEventListener" statement, I encounter the error listed in the title. The error message (complete) is:
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at asdata::Bumper()
at bumper_fla::MainTimeline/frame1()
The main file code is:
import asdata.Bumper;
var myBumper:Bumper = new Bumper();
The class package file is:
ActionScript Code:
package asdata{
import flash.display.*;
import flash.net.URLRequest;
import flash.events.*;
public class Bumper extends Sprite{
private var bumper:Sprite = new Sprite();
private var loader:Loader = new Loader();
private var rq:URLRequest = new URLRequest("bumper.png");
public function Bumper() {
trace("entered new bumper creation");
loader.load(rq);
this.loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadBumper);
//next statement is where the error occurs.
//I think it's having a problem accessing stage because
//it is null but I don't know what to do to fix this.
stage.addEventListener(MouseEvent.MOUSE_MOVE,moveBumper);
}
function loadBumper(event:Event):void {
var bumperImage:BitmapData = new BitmapData(loader.width,loader.height,false,0x000000);
bumperImage.draw(this.loader);
this.bumper.graphics.beginBitmapFill(bumperImage, null, true);
bumper.graphics.drawRect(0,0,100,10);
bumper.graphics.endFill();
bumper.x = 10;
bumper.y = 290;
addChild(bumper);
}
function moveBumper(e:MouseEvent):void {
bumper.x = e.stageX - bumper.width;
}
}
}
How do I substantiate stage before accessing it? Or am I analyzing this error incorrectly? Also, when I had the code all in one file, a picture actually appeared when it executed the 'addChild' statement. However, now that I've separted the code into 2 files (main with .as file), nothing appears after the addChild is executed. :x Am I wrong to assume that anything added to the display list in a class will not appear? What am I doing wrong here, too?
Any help is appreciated,
Marion the Mighty Confused
P.S. I'm sorry the program isn't indented correctly, the editor stripped out my tabs and extra spaces.