Skip to main content
Participant
June 20, 2011
Question

Unload external content - Null object error

  • June 20, 2011
  • 1 reply
  • 519 views

Hi Ladies and Gents;

I'm new around here, so please bear with me. I know this is a commonly-experienced problem, and I hate to post ANOTHER topic about it, but after three days of reading over dozens of topics and trying to correct this on my own, I'm at a loss. I know I'm doing something wrong, but I don't understand AS3 well enough yet to know exactly what.

I have a parent swf and a child swf. I click a button on the parent stage to open the child swf. I've been trying to make a "return to main" type of button that can be clicked to unload the child swf and bring me back to the parent swf.

Here is the code for the parent:

var myLoader:Loader=new Loader(); qual_btn.addEventListener(MouseEvent.CLICK, qualifications); function qualifications(myevent:MouseEvent):void {     var myURL:URLRequest = new URLRequest("qualifications.swf");     myLoader.load(myURL);     addChild(myLoader);     myLoader.content.addEventListener('killMe', killLoadedClip); } function killLoadedClip(event:Event):void {     event.target.removeEventListener('killMe', killLoadedClip);     stage.removeChild(myLoader);     myLoader.unload(); }

And here is what I have for the child:

back_btn.addEventListener(MouseEvent.CLICK, dispatchEvent);
dispatchEvent(new Event("killMe", true));

What's been happening is that I'm getting a null object reference:

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

I know the issue is with line 8 of the parent AS:

   myLoader.content.addEventListener('killMe', killLoadedClip);

But I don't understand what's wrong with it, or why it's giving me a null object reference.

Please, any advice you can offer would be very, very welcome. I've been at this for three days, but I can't fix it when I don't quite understand what 'it' is that needs fixing.

Thank you.

-Batty

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
June 20, 2011

The 1009 error indicates that one of the objects being targeted by your code is out of scope.  This could mean that the object....


 
- is not in the display list
- doesn't have an instance name (or the instance name is mispelled)
- does not exist in the frame where that code is trying to talk to it
- is animated into place but is not assigned instance names in every keyframe for it
- is one of two or more consecutive keyframes of the same objects with no name assigned in the preceding frame(s).
 
If you go into your Publish Settings Flash section and select the option to Permit debugging, your error message should have a line number following the frame number which will help you isolate which object is involved.
batty_kAuthor
Participant
June 20, 2011

Hi Ned;

Thank you. I've permitted debugging, and it tells me that the code in frame 147, line 8 is the problem.

Line 8 is thus:

    myLoader.content.addEventListener('killMe', killLoadedClip);

So I'm still not entirely sure which object is involved, as at this point everything on the stage is loaded. Should everything in my file have its own instance name? Would that help to correct the problem?

Ned Murphy
Legend
June 20, 2011

If that's from the code you showed up top, it is more likely that nothing is loaded by the time you are trying to add the event listener to the Loader.content.  Loading takes time, and the code in your function doesn't wait for the loading to complete before moving on, so you need to assign a listener to wait for the loading to complete.

function qualifications(myevent:MouseEvent):void
{
    var myURL:URLRequest = new URLRequest("qualifications.swf");
    myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, assignKiller);

    myLoader.load(myURL);
    addChild(myLoader);
}

function assignKiller(evt:Event):void {

    myLoader.content.addEventListener('killMe', killLoadedClip);

}