Skip to main content
Known Participant
March 26, 2013
Question

What is causing this error 1009?

  • March 26, 2013
  • 1 reply
  • 1099 views

I am trying to organize and neaten my code, but as I shifted things around I began to get error #1009: Cannot access a property or method of a null object reference. After around 2 hours of trying to figure out what was null, I had to turn to the forums because I am completely lost. Being relatively new to the whole flash scene, I was hoping some of you guys could help me figure it out. (And if anybody could please tell me how to do code in forum posts here, that would also be good for you and me )

[code]

package  {

    import flash.display.MovieClip;

    import flash.events.Event;

    import flash.display.Sprite;

    import flash.geom.ColorTransform;

    import flash.display.DisplayObject;

    import flash.text.TextField;

    import flash.display.Stage;

    public class Main extends MovieClip {

        private static var _instance:Main = null;

        var upgradeScreen:UpgradeScreen;

        var btn:ButtonCooldown;

        var money:int = 0;

        var maxCoolDown:Number = 10;

        var coolDownSpeed:Number = .1;

        var coolDownIncrease:Number = 2;

        public const COOL_SPEED:Number = 0.05;

        var screen:int = 0;

        var screenElements:Array = new Array();

        var btnWidth:Number = 200;

        var btnHeight:Number = 40;           

        var extBtnWidth:Number = 100;

        var extBtnHeight:Number = 20;

        var hasInit:Boolean = false;

        var gameScreenElements:Array =

        [

            new ButtonGame(stage.stageWidth - btnWidth - 10, stage.stageHeight - btnHeight - 10, exitGame, btnWidth, btnHeight, "EXIT"),

            new ButtonGame(10, stage.stageHeight - btnHeight - 10, openUpgrades, btnWidth, btnHeight, "Upgrades")

        ]

        var titleScreenElements:Array =

        [

            new ButtonGame(stage.stageWidth / 2 -btnWidth / 2, stage.stageHeight / 2 - btnHeight / 2, startGame, btnWidth, btnHeight, "Start game!"),

            new ButtonGame(stage.stageWidth / 2 -btnWidth / 2, stage.stageHeight / 2 - btnHeight / 2 + btnHeight + 30, startGame, btnWidth, btnHeight, "test"),

        ]

        public function Main()

        {

            _instance = this;

            setupTitleScreen();

            this.addEventListener(Event.ENTER_FRAME, updateGame);

            removeChild(hud);

            upgradeScreen = new UpgradeScreen();

            upgradeScreen.init();

        }

        public static function getInstance():Main { return _instance; }

        public static function getStage():Stage { return getInstance().stage; }

        public function someFunction(amt:Number)

        {

            money += amt;

        }

        private function updateGame(e:Event):void

        {

            if(!hasInit)

            {

                init();

                hasInit = true;

            }

            hud.moneyText.text = "$" +String((money / 100).toFixed(2));

            for(var i:int = 0; i < screenElements.length; i++)

            {

                if(screenElements is ButtonGame || screenElements is ButtonSlider)

                {

                    screenElements.update();

                }

            }

        }

        private function clearScreen():void

        {

            graphics.clear();

            for(var i:int = 0; i < screenElements.length; i++)

            {

                removeChild(screenElements);

            }

            screenElements.splice(0, screenElements.length);

        }

        private function setupGameScreen():void

        {

            clearScreen();

            addArrayToScreen(gameScreenElements);

            addElementToScreen(hud);

        }

        private function setupTitleScreen():void

        {

            clearScreen();

            addArrayToScreen(titleScreenElements);

        }

        private function setupUpgradeScreen():void

        {

            clearScreen();

            drawUpgradeBG();

            if(upgradeScreen == null)

            {

                trace("upgradeScreen is null");

            }

            addArrayToScreen(upgradeScreen.upgradeScreenElements);

        }

        public function addElementToScreen(obj:DisplayObject):void

        {

            screenElements[screenElements.length] = obj;

            addChild(obj);

        }

        private function addArrayToScreen(ary:Array):void

        {

            for(var i:int = 0; i<ary.length; i++)

            {

                addElementToScreen(ary);

            }

        }

        public function startGame(btn:ButtonGame):void

        {

            setupGameScreen();

        }

        private function exitGame(btn:ButtonGame)

        {

            setupTitleScreen();

        }

        private function openUpgrades(btn:ButtonGame):void

        {

            setupUpgradeScreen();

        }

        private function drawUpgradeBG():void

        {

            this.graphics.beginFill(0x8e8e8e, 1.0);

            this.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);

        }

        public function getButtonArray():Array

        {

            var array:Array = new Array();

            for(var i:int = 0; i<gameScreenElements.length; i++)

            {

                if(gameScreenElements is ButtonCooldown)

                {

                    array[array.length] = gameScreenElements;

                }

            }

            return array;

        }

    }

}

[/code]

There is a lot here, I didn't know what to include or remove, so I kept everything just in case. I highlighted the most important areas. It is also saying the error is occuring in the constructor of UpgradeScreen, but there is nothing in it. I seperated that out and put it into an init() function to test what was causing it. Here is the code for that class anyway

[code]

package 

{

    import flash.text.TextField;

    import flash.text.Font;

    import flash.text.TextFormat;

    import flash.text.TextFormatAlign;

    import flash.text.TextFieldType;

    import flash.display.Stage;

    public class UpgradeScreen

    {

        var futuraFont:Font = new FuturaBold();

        var main:Main;

        var btnWidth:Number = 200;

        var btnHeight:Number = 40;           

        var extBtnWidth:Number = 100;

        var extBtnHeight:Number = 20;

        var stageObj:Stage;

        var upgradeScreenElements:Array =

        [

            new ButtonGame(stageObj.stageWidth / 2 -extBtnWidth / 2, stageObj.stageHeight - extBtnHeight, main.startGame, extBtnWidth, extBtnHeight, "Back to game"),           

            new ButtonSlider(stageObj.stageWidth - ScrollItem.getWidth() - 30, 40, stageObj.stageHeight - 40),

            new ButtonGame((stageObj.stageWidth - upgradeScreenElements[1].width) / 2 + 50, 10, addButton, 100, 35, "New Button")

        ]

        public function UpgradeScreen()

        {

               //Why is the error here??

        }

        public function init():void

        {

            this.stageObj = Main.getStage();

            this.main = Main.getInstance();

        }

        private function addButton(btn:ButtonGame)

        {

            main.gameScreenElements[main.gameScreenElements.length] = new ButtonCooldown(main.getButtonArray.length, main.someFunction);

        }

    }

}

[/code]

Thank you in advance. I'm about to tear my hair out. Want to get back to programming

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
March 26, 2013

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 declared but not instantiated

- 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.

ZXZ1661Author
Known Participant
March 26, 2013

Thank you very much for the quick reply!! I had been looking for a more extensive output for awhile now, thank you for that! Will see what I can do now, thanks for the info.

Ned Murphy
Legend
March 26, 2013

I have been pasting that message for so many years I forget when it is less applocable... Ignore my comments regarding frames and the line number following the frame number since this involves an external class file.  The error should hopefully still point to a line number in the .as file.  Post back if you remain baffled with it.