Loading swf + error #1009

I am trying to load an swf, but i get this error:


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

all the code is in the timeline, i tried using Event.INIT and Event.COMPLETE as the event type for my Loader, I have removed all references to ‘stage’ in the loaded swf, and it plays fine when tested.
I don’t know what’s wrong. Can someone tell me what’s wrong?

I am using a movieclip in the library with linkage names… anyone?

Can you post the code or source? That error isn’t much to go on by itself.

Try waiting to run your constructor in the loader SWF until the ADDED_TO_STAGE event. That’s a common cause of the error.

here’s the code in the loaded swf.


var total:int = 4;
var count:int = 1;
var timer:Timer = new Timer(1000, total);
var boxes:Array = new Array();
var lines:Array = new Array();

var stage_width:Number = 430;
var stage_height:Number = 280;

init();

function init():void{
	createBox();
	timer.start();
	timer.addEventListener(TimerEvent.TIMER, delayedCreateBox);
	timer.addEventListener(TimerEvent.TIMER_COMPLETE, enableClickable);
}
function reset(e:KeyboardEvent):void{
	if(e.keyCode == Keyboard.SPACE){
		timer.stop();
		timer.reset();
		for(var i:int = 0; i < boxes.length; i++){
			removeChild(boxes*);
		}
		boxes = new Array();
		count = 1;
		init();
	}
}
function createBox():sbBox{
	var b:sbBox = new sbBox();
	b.percentX = Math.random();
	b.percentY = Math.random();
	b.targX = Math.random();
	b.targY = Math.random();
	b.scaleX = .01;
	b.scaleY = .01;
	b.rotation = Math.random() * 180;
	b.alpha = .2 + Math.random();
	b.x = stage_width * b.percentX;
	b.y = stage_height * b.percentY;
	
	b.addEventListener(Event.ENTER_FRAME, scaleBox);
	
	addChild(b);
	boxes.push(b);
	return b;
}
function scaleBox(e:Event):void{
	var targSize:Number = .2 + Math.random();
	e.target.scaleX += (targSize - e.target.scaleX) * .2;
	e.target.scaleY += (targSize - e.target.scaleY) * .2;
	if(e.target.scaleX >= targSize){
		e.target.removeEventListener(Event.ENTER_FRAME, scaleBox);
		e.target.addEventListener(Event.ENTER_FRAME, moveX);
		e.target.addEventListener(Event.ENTER_FRAME, moveY);
	}
}
function delayedCreateBox(e:Event):void{
	var p:sbBox = boxes[boxes.length - 1];
	for(var i:int = 0; i < count; i++){
		var b:sbBox = createBox();
	}
	count += i;
}
function moveX(e:Event):void{
	e.target.x += (e.target.targX * stage_width - e.target.x) * .1;
	if(Math.abs((e.target.targX * stage_width) - e.target.x) <= 1 ){
		e.target.targX = Math.random();
	}
															   
}
function moveY(e:Event):void{
	e.target.y += (e.target.targY * stage_height - e.target.y) * .1;
	if(Math.abs((e.target.targY * stage_height) - e.target.y) <= 1 ){
		e.target.targY = Math.random();
	}
}

function enableClickable(e:TimerEvent):void{
	
}

what should listen for the ADDED_TO_STAGE event? it’s a website, so the loader swf will need to run first before the loaded swf.
spBox is a linkage name of a movieclip on the stage…

Try changing the “init()” line to reading:


addEventListener(Event.ADDED_TO_STAGE, init);

^ just tried that in the timeline code… added an e:Event = null parameter to init(), still the same error… :-/

Try to narrow it down to which line is causing the error, by commenting out all lines and then uncommenting blocks at a time.

is it possible that the MovieClip with the linkage name is not initiated?
because other than the class sbBox, there’s nothing in that code but numbers, functions and events…

just tried what was suggested in this thread, still the same error. :-/

Try commenting out lines until you find the one that is causing the error. Specifically, a good place to start is that addChild line, and anything else that might be accessing the stage. If it works perfectly when not being loaded, it’s almost always a stage access problem.

This is sort of an important debugging skill to learn - finding the source of the error by working with smaller chunks until you get it figured out.

If you can narrow down the error to a certain chunk, you can use a trace statement every other line (or however often makes sense) to see after which trace statement the code breaks down (watch your output and make each trace statement unqiue).

use Debugging mode.
Ctrl + Shift + Enter
There you will now where is the problem