# Preloader loads swf - main swf giving error

**URL:** <https://forum.kirupa.com/t/preloader-loads-swf-main-swf-giving-error/261687>\
**Category:** flash\
**Created:** [May 29, 2008, 9:04pm UTC](https://forum.kirupa.com/t/preloader-loads-swf-main-swf-giving-error/261687 "2008-05-29T21:04:11Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![Digitalosophy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/digitalosophy/32/1114_2.png) [@Digitalosophy](https://forum.kirupa.com/u/Digitalosophy)\
**Post date:** [May 29, 2008, 9:04pm UTC](https://forum.kirupa.com/t/preloader-loads-swf-main-swf-giving-error/261687/1 "2008-05-29T21:04:11Z")

</div>

So I have a preloader.swf that loads index.swf

index.swf document class is Main.as

Main.as takes a movieclip from the library and adds it to the stage (tile bg tutorials only as 3 version).

When index is compiled it works fine. Once I complie preloader, I get “TypeError: Error #1009: Cannot access a property or method of a null object reference.”.

Once I comment out the addChild line everything is fine. So I’m assuming it had to do with scope.

Here’s preloader code

```auto

stop ();

import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.net.URLRequest;
import flash.text.TextField;
import flash.events.Event;
import flash.events.ProgressEvent;

var myLoader:Loader = new Loader();
var myRequest:URLRequest = new URLRequest("index.swf");
var loadProgress_txt:TextField = new TextField();

myLoader.load (myRequest);

myLoader.contentLoaderInfo.addEventListener (Event.OPEN,showPreloader);
myLoader.contentLoaderInfo.addEventListener (ProgressEvent.PROGRESS,showProgress);
myLoader.contentLoaderInfo.addEventListener (Event.COMPLETE,showLoadResult);

function showPreloader (evt:Event):void {
    addChild (loadProgress_txt);
}

function showProgress (evt:ProgressEvent):void {
    //loadProgress_txt.text = "loaded:”+evt.bytesLoaded+" from "+evt.bytesTotal;
}

function showLoadResult (evt:Event):void {
    removeChild (loadProgress_txt);
    addChild (myLoader);
}

```

Main.as

```auto

package {
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    public class Main extends Sprite {

        //
        //
        //
        //
        private var tileWidth:Number;
        private var tileHeight:Number;
        private var xMax:Number;
        private var yMax:Number;
        private var sHeight:Number;
        private var sWidth:Number;
        private var bg:Sprite;
        //
        //
        //
        //
        public function Main () {
            tileWidth = 36;
            tileHeight = 36;
            sHeight = stage.stageHeight;
            sWidth = stage.stageWidth;
            xMax = Math.round(sHeight/tileWidth);
            yMax = Math.round(sWidth/tileHeight);
            for (var i:int = 0; i<= xMax + 1; i++) {
                for (var j:int = 0; j <= yMax + 1; j++) {
                    var myPattern:Pattern = new Pattern();
                    addChild (myPattern); // this is the line
                    myPattern.x = tileHeight * j;
                    myPattern.y = tileWidth * i;
                }
            }
            
        }
    }
}

```

I tried simple putting a this.addChild(myPattern) but that doesn’t work. I’m hoping someone can explain this to me.

TIA

---

<div class="post-metadata">

**Author:** ![rbnzdave](https://avatars.discourse-cdn.com/v4/letter/r/a587f6/32.png) [@rbnzdave](https://forum.kirupa.com/u/rbnzdave)\
**Post date:** [May 29, 2008, 9:24pm UTC](https://forum.kirupa.com/t/preloader-loads-swf-main-swf-giving-error/261687/2 "2008-05-29T21:24:37Z")

</div>

“this” in the context you have ( inside an event listener ) is an event, not the parent movieclip.

trace (this), then this.parent, then this.parent.parent until you get the object you are expecting. another way is to set

```auto
var home:MovieClip = this;

```

at the top with your declarations, then use

```auto
home.addChild(whatever);

```

I used to get lost in events all the time. now I just get lost in functions, which is much more satisfying stress. 🙂

---

<div class="post-metadata">

**Author:** ![Digitalosophy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/digitalosophy/32/1114_2.png) [@Digitalosophy](https://forum.kirupa.com/u/Digitalosophy)\
**Post date:** [May 30, 2008, 12:45am UTC](https://forum.kirupa.com/t/preloader-loads-swf-main-swf-giving-error/261687/3 "2008-05-30T00:45:13Z")

</div>

The did clear up some things so thank you for that. Although that did not fix my problem. I’m not sure if I am just looking at this too long or what :lol:

I’ll give it a go tomorrow morning, and I thank you for your help.

---

<div class="post-metadata">

**Author:** ![Digitalosophy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/digitalosophy/32/1114_2.png) [@Digitalosophy](https://forum.kirupa.com/u/Digitalosophy)\
**Post date:** [May 30, 2008, 2:10pm UTC](https://forum.kirupa.com/t/preloader-loads-swf-main-swf-giving-error/261687/4 "2008-05-30T14:10:29Z")

</div>

Ok so.

```auto

					trace(this.parent);
					this.parent.addChild (myPattern);

```

this.parent trace to [object Stage], which is correct, right?

Because I want to add the movieclip to the stage scope not the event scope.

So when I test it alone of course it works just fine. But once I compile my preloader swf I get

```auto

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

```

Which isn’t making much sense to me. It appears that preloader.swf needs and object reference to the movieclip being attached inside it’s child swf. Why? main.swf is it’s own scope, correct?

_bangs head_

---

<div class="post-metadata">

**Author:** ![Digitalosophy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/digitalosophy/32/1114_2.png) [@Digitalosophy](https://forum.kirupa.com/u/Digitalosophy)\
**Post date:** [May 30, 2008, 7:04pm UTC](https://forum.kirupa.com/t/preloader-loads-swf-main-swf-giving-error/261687/5 "2008-05-30T19:04:22Z")

</div>

shameless bump 🙂

---

<div class="post-metadata">

**Author:** ![creatify](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@creatify](https://forum.kirupa.com/u/creatify)\
**Post date:** [May 31, 2008, 12:07am UTC](https://forum.kirupa.com/t/preloader-loads-swf-main-swf-giving-error/261687/6 "2008-05-31T00:07:30Z")

</div>

EDIT - trying one other thing right now…

I’m getting used to as3 myself… the issue is the stage references, and more so, I believe that they’re run in the constructor of your class. if you trace(stage) when its loaded into the other swf, you’ll notice it traces null.

I ran a test and this did NOT work when loading index.swf thought I’d bring it up: There is an addedToStage event - so when loading, instead of using Event.COMPLETE, I tried:  
ActionScript Code:  
[LEFT]myLoader.[COLOR=#000080]contentLoaderInfo[/COLOR].[COLOR=#000080]addEventListener[/COLOR] COLOR=#000000[/COLOR];  
[/LEFT]

But, stage is still null. So, when its loading into another swf, the stage property of your Main class isn’t being set before your constructor runs.

I can’t say this is the proper way - but attached are how I would set this up - I’d use the Doc class to generate the building blocks of my swf, but only the building blocks, not actual graphic execution. Files attached, hope this helps and if you find a more proper way around that issue, please post, thanks!

---

<div class="post-metadata">

**Author:** ![creatify](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@creatify](https://forum.kirupa.com/u/creatify)\
**Post date:** [May 31, 2008, 12:14am UTC](https://forum.kirupa.com/t/preloader-loads-swf-main-swf-giving-error/261687/7 "2008-05-31T00:14:00Z")

</div>

ummmm, I overthought that last one - this works too:

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [May 31, 2008, 12:28am UTC](https://forum.kirupa.com/t/preloader-loads-swf-main-swf-giving-error/261687/8 "2008-05-31T00:28:52Z")

</div>

Hey folks. The issue you’re having is that the **child** swf is accessing the stage before the loader has been loaded to the stage. The document class is a great place to do graphics and such - so the best way to fix this issue is to make your constructor look like this:

```auto

public function MyConstructor()
{
     addEventListener(Event.ADDED_TO_STAGE, init);
}

```

Now put whatever you had inside your constructor into the init() function, and you should be good to go. :thumb:

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [May 31, 2008, 12:32am UTC](https://forum.kirupa.com/t/preloader-loads-swf-main-swf-giving-error/261687/9 "2008-05-31T00:32:43Z")

</div>

So Digi, your code would become the following:

```auto

package 
{
    import flash.display.*;
    import flash.events.*;
    import flash.utils.*;
    public class Main extends Sprite 
	{
        //
        //
        //
        //
        private var tileWidth:Number;
        private var tileHeight:Number;
        private var xMax:Number;
        private var yMax:Number;
        private var sHeight:Number;
        private var sWidth:Number;
        private var bg:Sprite;
        //
        //
        //
        //
        public function Main () 
		{
            addEventListener(Event.ADDED_TO_STAGE, init);
        }
		
		private function init():void
		{
			tileWidth = 36;
            tileHeight = 36;
            sHeight = stage.stageHeight;
            sWidth = stage.stageWidth;
            xMax = Math.round(sHeight/tileWidth);
            yMax = Math.round(sWidth/tileHeight);
            for (var i:int = 0; i<= xMax + 1; i++) {
                for (var j:int = 0; j <= yMax + 1; j++) {
                    var myPattern:Pattern = new Pattern();
                    addChild (myPattern); // this is the line
                    myPattern.x = tileHeight * j;
                    myPattern.y = tileWidth * i;
                }
            }
		}
    }
}

```

That should fix the issue.

---

<div class="post-metadata">

**Author:** ![Digitalosophy](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/digitalosophy/32/1114_2.png) [@Digitalosophy](https://forum.kirupa.com/u/Digitalosophy)\
**Post date:** [May 31, 2008, 1:57am UTC](https://forum.kirupa.com/t/preloader-loads-swf-main-swf-giving-error/261687/10 "2008-05-31T01:57:35Z")

</div>

I thank you both very much for the help. :thumb:

---

<div class="post-metadata">

**Author:** ![creatify](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@creatify](https://forum.kirupa.com/u/creatify)\
**Post date:** [May 31, 2008, 3:30am UTC](https://forum.kirupa.com/t/preloader-loads-swf-main-swf-giving-error/261687/11 "2008-05-31T03:30:32Z")

</div>

Thanks for clarifying anogar! Thats where I finally arrived at on my last set of posted files… lots to learn with AS3. So you got away from sunny SD and the other s workplace eh?

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [May 31, 2008, 6:51am UTC](https://forum.kirupa.com/t/preloader-loads-swf-main-swf-giving-error/261687/12 "2008-05-31T06:51:32Z")

</div>

I transferred up to our Bellevue office actually, same job - new location. I love it up here so far, been about 4 months. 🙂
