Link to home
Start Free TrialLog in
Avatar of Sci-Fi-Si
Sci-Fi-SiFlag for United Kingdom of Great Britain and Northern Ireland

asked on

AS3 Cannot access a property or method of a null object reference.

Dear Experts!

I've almost finished the front end to my website. I'm now adding a bit of polish so I don't get Flash  displaying a white background for ages while everything loads.

To do this, I'm working on a preloader that loads a .swf into a MovieClip (or rather casts loader.content into a MovieClip)

My main .swf works fine when it's compiled and run on it's own, but when I load it into another MovieClip I get the following error:

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

Hopefully I've overlooked something really simple, any thoughts much appreciated :)
Avatar of scooby_56
scooby_56
Flag of United Kingdom of Great Britain and Northern Ireland image

The error indicates you are trying to access some property of an object not yet created or that doesn't exist for thay object type. This could simply be a typo. Post some code for further help.  
Avatar of Sci-Fi-Si

ASKER

The trouble is the code works fine when compiled and run as it's own .swf. Its just when I load it into a MovieClip it breaks.
The loader I'm using is pasted below which casts the .swf to a MovieClip called main. The .swf main code breaks as soon as I make the very first function call to newObject();
TypeError: Error #1009: Cannot access a property or method of a null object reference.
      at movie/newObject()
      at movie/frame1()
Its as though the main.swf just doesn't like being abstracted into another MovieClip?

//Load main movie.
var main:MovieClip;
 
var thisLoader:Loader = new Loader();
thisLoader.contentLoaderInfo.addEventListener(Event.INIT, doneMain);
thisLoader.load( new URLRequest("boot/main.swf") );
 
function doneMain(e:Event):void
{
	//Acknowledged, now remove event trigger
	thisLoader.contentLoaderInfo.removeEventListener(Event.INIT, doneMain);
	//Cast loaded element to a MovieClip
	main = MovieClip(thisLoader.content);
	stage.addChild(main);
	
	//Set alpha to fade in and position on stage
	main.alpha = 0;
	//positionLogo();
	Tweener.addTween(main, {alpha:1, time:1, transition:"linear"});//thisMovieClip.stop();
}

Open in new window


Thanks scooby!

I think there is something spurious about the way I'm managing my variables. It's ok as a stand-alone movie, but not as part of another MovieClip.
I'm guessing the way I'm declaring i:uint on the main timeline... to then be abstracted.... humm

var object = new Array ();
var numObjects:uint = 100;
var i:uint;
 
for (i = 0; i < numObjects; i++)
{
 newObject(i);
}
 
function newObject(i):void
{
 object[i] = new Object();
}

Open in new window

Yes I just tried the COMPLETE, but it didn't change anything...

I read yesterday that INIT was the one to use when loading a .swf as it was a case of not just having finished loading but the MovieClip actually INITialising...
i think this should be fine.
I created a AS3 swf with only the loop above and added some traces. named 'main'
Then created and copied and pasted the preloader in your second post.
Ran the preloader and got the traces from main absolutely fine. The only line i had to comment was the Tweener.
Would it be feasable to ether add a whole boat load of traces in main.fla so you can see how far execution is going, or try to recreate issue with minimum code as i have.?
Nice one scoob :)
I've just stripped it down to a few lines and every thing's fine.

I'd have to upload all the .swf's, bootloader and directory structure you you to be able to get the error I'm getting.

Will post soon when I can find out a bit more...

Thank for your replies.

:)
please post in CS3 compatibility :)
Okey Dokey,

Have stripped the two files down as much as can be and am still getting the same error - which is good, at least narrowing it down.

Both files are for CS3 and  set to compile for Flash 9 and work in the same directory.
If I compile and run the starroutine.fla (I don't get the 3D) but they do twinkle. So at least I can see its working
If I compile and run the bootstrap. It loads the starrouting.swf and then the screen is blank with the same error:

TypeError: Error #1009: Cannot access a property or method of a null object reference.

Many thanks Scooby

bootstrap.fla.txt
starroutine.fla.txt
problem is with this line.
 star[i].x = Math.random() * (stage.stageWidth << 2) - (stage.stageWidth << 1);

stage is null
change these values to hardcoded and voila... no error
 star[i].x = Math.random() * 50;
 
ASKER CERTIFIED SOLUTION
Avatar of scooby_56
scooby_56
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Oh, you star!

Honestly, that's a real gotcha' it's only going to ever occur when its being loaded from another .swf (ie. a pre-loader) and has no reference to the stage!

Nice one scooby dooby doo!
 
You little beauty!