# 1119: Access of possibly undefined

**URL:** <https://forum.kirupa.com/t/1119-access-of-possibly-undefined/314667>\
**Category:** Uncategorized\
**Created:** [October 5, 2010, 10:43pm UTC](https://forum.kirupa.com/t/1119-access-of-possibly-undefined/314667 "2010-10-05T22:43:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![rimacy](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@rimacy](https://forum.kirupa.com/u/rimacy)\
**Post date:** [October 5, 2010, 10:43pm UTC](https://forum.kirupa.com/t/1119-access-of-possibly-undefined/314667/1 "2010-10-05T22:43:14Z")

</div>

here’s the whole message:

```auto
1119: Access of possibly undefined property screenlayer through a reference with static type ScreenHandler.

```

here’s the order

document\>screens\>screenLayer\>menuScreen\>buttonToIntroScreen \>

> transmits “introScreen” \>document layer is sposed to listen but when i try to add the listener i get the error, below are the pertinent bits O code:

in document: (check the underLined parts)

```auto
package
{
    import flash.display.MovieClip;
    import flash.events.Event;
    import flash.events.ProgressEvent;
    import traceDL;
    
    public class Document extends MovieClip
    {
        public var loadingProgress:LoadingProgress;
        public static var screens:ScreenHandler = new ScreenHandler();
        var menuScreen:MenuScreen;
        var introScreen:IntroScreen=new IntroScreen;

        public function Document():void
        {
            loadingProgress = new LoadingProgress();
            loadingProgress.x = this.parent.width/2;
            loadingProgress.y = this.parent.height/2;
            addChild( loadingProgress );
            loaderInfo.addEventListener( Event.COMPLETE, onCompletelyDownloaded );
            loaderInfo.addEventListener( ProgressEvent.PROGRESS, onProgressMade );
            //this.addChild(screens);
            //Application.screens.switchTo("SelectedScreen");
            //Application.screens.switchTo("SelectedScreen", false);

        
        }
        public function onProgressMade( progressEvent:ProgressEvent ):void
        {
            loadingProgress.setValue( Math.floor( 100 * loaderInfo.bytesLoaded / loaderInfo.bytesTotal ) );
            
        }
        public function onCompletelyDownloaded( event:Event ):void
        { removeChild(loadingProgress);
            gotoAndStop(3);
            playGame();
        }
        public function playGame():void
        {
            
            var menuScreen:MenuScreen=new MenuScreen;
            this.addChild(screens);//adding the screens(ScreenHandlerClass)
            trace("screens children=" + screens.getChildAt(0));
            screens.addChild(introScreen);//if i dont add here nothing shows up
            introScreen.buttonToMenuScreen.addEventListener("menuScreen",handleToMenuScreen);//listening for the buttons event
            [U]screens.screenlayer.MenuScreen.buttonToIntroScreen.addEventListener("introScreen",handleToMenuScreen);[/U]
            loaderInfo.removeEventListener( Event.COMPLETE, onCompletelyDownloaded );
            loaderInfo.removeEventListener( Event.COMPLETE, onProgressMade );
        

        }
        public function handleToMenuScreen(evt:Event):void
        {
            //for (var i:uint = 0; i < screens.numChildren; i++)
// {
// trace ('	| ' +i+'. name:' + screens.getChildAt(i).name + ' type:' + typeof (screens.getChildAt(i))+ '	' + screens.getChildAt(i));
// }
            trace("eventHeard");
            screens.removeChild(introScreen);
            var eventString:String=new String(evt.type);
            trace(evt.type + "handleToMenuScreen");
            screens.switchTo(eventString);
        }
    }
}

```

in screenHandler:

```auto
package {
    import flash.display.Sprite;
    import flash.display.MovieClip;
    import flash.events.Event;

    public class ScreenHandler extends Sprite{
        public var menuScreen:MenuScreen;///all these were private
        private var playScreen:PlayScreen;
        private var mBase:MBase;
        private var asBase:AsBase;
        private var introScreen:IntroScreen;
        
        private var newScreenName:String = "";
        //private var oldScreenName:String ="introScreen";
        
        public var screenLayer:Sprite = new Sprite();
        
        
        public function ScreenHandler() 
        {
            //trace("screenHandler");
            this.addChild(screenLayer);
            //for (var i:uint = 0; i < numChildren; i++)
// {
// trace ('	| ' +i+'. name:' + getChildAt(i).name + ' type:' + typeof (getChildAt(i))+ '	' + getChildAt(i));
// }

            
        }

```

in menuScreen:

```auto
package
{
    import flash.display.MovieClip;
    public class MenuScreen extends MovieClip
    {
        
        var buttonToIntroScreen:Button1=new Button1("introScreen")
        
        public function MenuScreen():void
        {
            buttonToIntroScreen.x=100;
            buttonToIntroScreen.y=100;
            addChild(buttonToIntroScreen);
            
            //trace("menuScreenHere");
            //needs buttons that effect :fuel repair goods
            //needs to show current weight and efficiency
            //needs to show btrace("");ank account
            //needs to choose route
            //needs a button that starts the level
            //start level x
            
        }
    
        
    }
}

```

in button1:

```auto
package
{
    import flash.display.*;
    import flash.events.*
    
    public class Button1 extends Sprite
    {
        var fireString:String = "";
        var fStringHolder:String ="";
        
        //Since you will recreate the event/folder with information each time the event happens, 
        //there is no use holding a reference to it.
        //var eventFolder:Event//=new Event(fireEvent);mayBe move this part into thefunction;
        
        public function Button1 (fireEvent:String):void
        {
            //var fString:String=fireEvent;
            
            fireString = fireEvent;
            fStringHolder=fireEvent;
            //trace(fireString + "is fireString");
            
            var button:SimpleButton= new SimpleButton();
            button.x=20;
            button.y=20;
            
            button.upState= createCircle(0x00ff00,15);
            button.overState= createCircle(0xffffff,16);
            button.downState= createCircle(0xcccccc,15);
            button.hitTestState= createCircle(0x00ff00,15);
            
            button.addEventListener(MouseEvent.CLICK,handleClick);
            
            addChild(button);
        }
        
        public function createCircle(color:uint,radius:Number):Shape
        {
            var circle:Shape = new Shape();
            circle.graphics.lineStyle(1,0x000000);
            circle.graphics.beginFill(color);
            circle.graphics.drawCircle(0,0,radius);
            circle.graphics.endFill();
            return circle;
            
        }
        private function handleClick(event:MouseEvent):void
        {
            trace("handleClick");
            //if(this.parent.parent != null)
// {
// var parentObj:Object = this.parent.parent.parent as Object;
// //parentObj.screens.switchTo(fireString);
// //Document.screens.switchTo(fireString);
//                
// }
            //trace("handleClick");
            
// trace(fireString);
            dispatchEvent(new Event(fireString));
            //trace("event Dispatched");
        }
    }
}

```

any ideas???
