Error #1009, ActionScript 3

2019-08-31 08:47发布

问题:

I am using a SWF(accueil) inside another SWF(ranchleblanc_fr) and this causes the error 1009 to appear:

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

The first SWF has a particle effect in it and it won't start when I first load the page. If i click on any button of the main page and return to the accueil page, the effect works. But if I click on another button again, I get another error 1009, but this time it does not only appear once. It appears over and over again until I close the SWF.

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

I will include the code, but before I do, I need to say that I checked the box to permit debugging in the publish settings, and I do not have a line number to locate the error. I used trace and found out that the null variable is flake. I searched the internet to figure out how to fix my problem, and I think I need to use an if statement so make sure that the program runs with a null variable. I think I should check if the flake variable is null and if it is, declare some kind of value to it.

The problem is that I do know how to code that if statement...

Here is my code for accueil_fla, frame 1 (where all the actions for this fla can be found)

for (var i:uint = 0; i < 50; i++)
{
    flake.x = Math.random() * stage.stageWidth;
    flake.y = Math.random() * stage.stageHeight;
    flake.alpha = randRange(.3,1);
    flake.scaleX = flake.scaleY = randRange(.3,1);
    addChild(flake);
} 

function randRange(low:Number, high:Number):Number
{
    var randNum:Number = Math.random() * (high - low) + low;
    return randNum;
}

this.addEventListener(Event.ENTER_FRAME, newFlake);

function newFlake(e:Event):void
{
    var flake:Snowflake = new Snowflake(randRange(-1,1), randRange(2,5));
    flake.x = Math.random() * stage.stageWidth;
    flake.y = -5;
    flake.alpha = randRange(.3,1);
    flake.scaleX = flake.scaleY = randRange(.3,1);
    addChild(flake);
}
var flake:Snowflake = new Snowflake(randRange(-1,1), randRange(2,5));

I can also include the code for the main page to fix why the snow particle effect does not work on the first load.

//----------------initialization---------------\\

extLoader_mc.visible = false;

//--------------------loader------------------------\\

var loader:Loader = new Loader;

loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, preload);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, done);

function preload (e:ProgressEvent):void
{
    extLoader_mc.visible = true;
    var pct:Number = e.bytesLoaded / e.bytesTotal;
    extLoader_mc.extLoaderBar_mc.scaleX = pct;
}

function done (e:Event):void
{
    extLoader_mc.visible = false;
    canvas_mc.addChild(loader);
}

//-----------------------end loader----------------\\


var firstLoad:URLRequest = new URLRequest("accueil.swf");
loader.load(firstLoad);
var currentPage:MovieClip;
currentPage=accueil_mc;
currentPage.gotoAndPlay("down");
accueilHit_mc.targetmc = accueil_mc;
aProposHit_mc.targetmc = aPropos_mc;
animauxHit_mc.targetmc = animaux_mc;
locationHit_mc.targetmc = location_mc;
contactHit_mc.targetmc = contact_mc;

accueilHit_mc.buttonMode = true;
aProposHit_mc.buttonMode = true;
animauxHit_mc.buttonMode = true;
locationHit_mc.buttonMode = true;
contactHit_mc.buttonMode = true;

function el () 
{
    accueilHit_mc.addEventListener(MouseEvent.CLICK, accueilPage);
    accueilHit_mc.addEventListener(MouseEvent.MOUSE_OVER, btnOver);
    accueilHit_mc.addEventListener(MouseEvent.MOUSE_OUT, btnOut);
    //aProposHit_mc.addEventListener(MouseEvent.CLICK, aProposPage);
    aProposHit_mc.addEventListener(MouseEvent.MOUSE_OVER, btnOver);
    aProposHit_mc.addEventListener(MouseEvent.MOUSE_OUT, btnOut);
    //animauxHit_mc.addEventListener(MouseEvent.CLICK, animauxPage);
    animauxHit_mc.addEventListener(MouseEvent.MOUSE_OVER, btnOver);
    animauxHit_mc.addEventListener(MouseEvent.MOUSE_OUT, btnOut);
    locationHit_mc.addEventListener(MouseEvent.CLICK, locationPage);
    locationHit_mc.addEventListener(MouseEvent.MOUSE_OVER, btnOver);
    locationHit_mc.addEventListener(MouseEvent.MOUSE_OUT, btnOut);
    contactHit_mc.addEventListener(MouseEvent.CLICK, contactPage);
    contactHit_mc.addEventListener(MouseEvent.MOUSE_OVER, btnOver);
    contactHit_mc.addEventListener(MouseEvent.MOUSE_OUT,btnOut);
}

function btnOver (e:MouseEvent):void 
{
    e.currentTarget.targetmc.gotoAndPlay("over");
}

function btnOut (e:MouseEvent):void 
{
    e.currentTarget.targetmc.gotoAndPlay("out");
}

function handCursor ()
{
    accueilHit_mc.useHandCursor = true;
    aProposHit_mc.useHandCursor = true;
    animauxHit_mc.useHandCursor = true;
    locationHit_mc.useHandCursor = true;
    contactHit_mc.useHandCursor = true;
    accueilHit_mc.enabled = true;
    aProposHit_mc.enabled = true;
    animauxHit_mc.enabled = true;
    locationHit_mc.enabled = true;
    contactHit_mc.enabled = true;
}
el();

accueilHit_mc.removeEventListener(MouseEvent.CLICK, accueilPage);
accueilHit_mc.removeEventListener(MouseEvent.MOUSE_OVER, btnOver);
accueilHit_mc.removeEventListener(MouseEvent.MOUSE_OUT, btnOut);

handCursor();

accueilHit_mc.useHandCursor = false;
accueilHit_mc.enabled = false;

function accueilPage (e:MouseEvent):void
{    
    currentPage.gotoAndPlay("out");            
    var req:URLRequest = new URLRequest("accueil.swf");
    loader.load(req);
    el();
    accueilHit_mc.removeEventListener(MouseEvent.CLICK, accueilPage);
    handCursor();
    accueilHit_mc.useHandCursor = false;
    accueilHit_mc.enabled = false;
    accueilHit_mc.removeEventListener(MouseEvent.MOUSE_OVER, btnOver);
    accueilHit_mc.removeEventListener(MouseEvent.MOUSE_OUT, btnOut);
    accueil_mc.gotoAndStop("down");
    currentPage=accueil_mc;
}

function aProposPage (e:MouseEvent):void
{
    currentPage.gotoAndPlay("out");    
    var req:URLRequest = new URLRequest("aPropos.swf");
    loader.load(req);
    el();
    aProposHit_mc.removeEventListener(MouseEvent.CLICK, aProposPage);
    handCursor();
    aProposHit_mc.useHandCursor = false;
    aProposHit_mc.enabled = false;
    aProposHit_mc.removeEventListener(MouseEvent.MOUSE_OVER, btnOver);
    aProposHit_mc.removeEventListener(MouseEvent.MOUSE_OUT, btnOut);
    aPropos_mc.gotoAndStop("down");
    currentPage=aPropos_mc;
}

function animauxPage (e:MouseEvent):void
{
    currentPage.gotoAndPlay("out");
    var req:URLRequest = new URLRequest("animaux_fr.swf");
    loader.load(req);
    el();
    animauxHit_mc.removeEventListener(MouseEvent.CLICK, animauxPage);
    handCursor();
    animauxHit_mc.useHandCursor = false;
    animauxHit_mc.enabled = false;
    animauxHit_mc.removeEventListener(MouseEvent.MOUSE_OVER, btnOver);
    animauxHit_mc.removeEventListener(MouseEvent.MOUSE_OUT, btnOut);
    animaux_mc.gotoAndStop("down");
    currentPage=animaux_mc;
}

function locationPage (e:MouseEvent):void
{
    currentPage.gotoAndPlay("out");
    var req:URLRequest = new URLRequest("location.swf");
    loader.load(req);
    el();
    locationHit_mc.removeEventListener(MouseEvent.CLICK, locationPage);
    handCursor();
    locationHit_mc.useHandCursor = false;
    locationHit_mc.enabled = false;
    locationHit_mc.removeEventListener(MouseEvent.MOUSE_OVER, btnOver);
    locationHit_mc.removeEventListener(MouseEvent.MOUSE_OUT, btnOut);
    location_mc.gotoAndStop("down");
    currentPage=location_mc;
}

function contactPage (e:MouseEvent):void
{
    currentPage.gotoAndPlay("out");        
    var req:URLRequest = new URLRequest("contact.swf");
    loader.load(req);
    el();
    contactHit_mc.removeEventListener(MouseEvent.CLICK, contactPage);
    handCursor();
    contactHit_mc.useHandCursor = false;
    contactHit_mc.enabled = false;
    contactHit_mc.removeEventListener(MouseEvent.MOUSE_OVER, btnOver);
    contactHit_mc.removeEventListener(MouseEvent.MOUSE_OUT, btnOut);
    contact_mc.gotoAndStop("down");
    currentPage=contact_mc;
}

Thanks again for your help it is much appreciated.

回答1:

Your error is most probably not really in your code, but in your project setup: You seem to reference a movie clip that is not on the stage at the time you're calling it.

You have to think of your timeline as an actual timeline: The frames represent time! If your button action triggers functions in another movie clip, it has to be on the stage at that exact moment - it is not enough to have it appear in a later frame (or in another scene, etc.etc.).

You should include null checks in your program, yes, but it probably would not work as expected anyway, even though it might no longer crash - where there is nothing that could execute a command, there will be no mechanism to actually do the job...

Also, once a null object reference error has been thrown (or any other error that went uncaught, for that matter), you should no longer use your program - it is broken, you need to fix it, before you can continue! And you certainly cannot trust any error messages after the first one.

Think of it like a clockwork: If at any point one of the cogwheels stopped turning, none of the other wheels could - but only one of them would be broken, causing all the others to malfunction as well. Only after you fix the first error, you can be sure that there really is another problem.



回答2:

Here is an article that explains why some AS3 SWFs work stand alone but fail to load into other SWFs. I hope this will be helpful for you: