Search code examples
actionscript-3classdynamic-text

Cannot access a property or method of a null object reference, when accessing a text field on stage


I'm trying to fill a dynamic text field on stage, but I'm getting the error above. the text field i'm trying to access exists on stage on frame 180. This class extends SimpleButton and it's a super class, where I have 5 subclasses which extends this class.

I also tried creating a new text instead of accessing an existed one, and I had the same error.

package  OOPGame{

    import flash.display.SimpleButton;
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import flash.text.*;

    public class Taste extends SimpleButton{

        internal static var sweetNum = 0; 
        internal static var bitterNum = 0; 
        internal static var sourNum = 0; 
        internal static var saltyNum = 0; 
        internal static var umamiNum = 0;
        internal static var recipeNum = 0;
        internal static var ingNum = 0;
        internal static var recipe:Array = new Array(3);
        internal static var recipeOne:Array = new Array;
        internal static var recipeTwo:Array = new Array;
        internal static var recipeThree:Array = new Array;
        internal static var recipeFour:Array = new Array;
        internal static var recipeFive:Array = new Array;
        protected var taste = "";
        protected var rOiO:TextField;

        public function Taste() {
            addEventListener(MouseEvent.CLICK, onClick);
        }

        public function getRecipe(obj:Object, num:int):void{
            recipe[num] = obj;
            trace("recipe: " + recipe);

        }

        protected function onClick(event:MouseEvent){
            if (DraggableIngredient.level == 4)
                MovieClip(root).gotoAndStop(76, "Scene 2");
            else {MovieClip(root).gotoAndStop(180, "Scene 2");
                  ++recipeNum;
                 // trace(getRecipe());
                       if (recipeNum == 1){
                           for (var i:Object in recipe){
                                recipeOne[ingNum] = recipe[i];
                                ++ingNum;}
                           recipeOne[ingNum] = taste;}
                       else if (recipeNum == 2){
                           for (i in recipe){
                                recipeTwo[ingNum] = recipe[i];
                                ++ingNum;}
                           recipeTwo[ingNum] = taste;}
                       else if (recipeNum == 3){
                            for (i in recipe){
                                recipeThree[ingNum] = recipe[i];
                                ++ingNum;}
                           recipeThree[ingNum] = taste;}

                trace("this's your first recipe "+recipeOne);
                //trace(recipeTwo);
                //trace(recipeThree);

                rOiO = parent.getChildByName("r1i1") as TextField;// here is the error
                //rOiO.text = recipeOne[0].toString();
                //rOiO = new TextField;
                 //parent.addChild(rOiO);
                }


        }


    }

}

the function getRecipe is called in another class just to fill the array recipe and it's working. it's getting me the following output:

this's your first recipe [object Orange],[object Apple],[object Tomato],Sweet
TypeError: Error #1009: Cannot access a property or method of a null object reference.
    at OOPGame::Taste/onClick()
    at OOPGame::Sweet/onClick()

and my subclasses looks like this:

package  OOPGame{

    import flash.events.MouseEvent;

    public class Sweet extends Taste{

        public function Sweet() {
            // constructor code
        }

        override protected function onClick(event:MouseEvent){
            ++sweetNum;
            taste = "Sweet";
            super.onClick(event);
        }
    }

}

Thanks in advance. Any help would be appreciated


Solution

  • You're comment indicates that you're getting an error when you attempt to access r1i1 from Sweet which extends Taste:

    rOiO = parent.getChildByName("r1i1") as TextField;

    The problem is before that line of code is executed, you are navigating away from the current frame on which Sweet exists:

    MovieClip(root).gotoAndStop(180, "Scene 2");

    My assumption is that the instance of Sweet does not exist on the display list of frame 180 of Scene 2, that being the case, parent is null. So the error is actually referencing parent, not r1i1.

    There are many ways to fix this problem. High on the list would be to not use scenes/frames to control the display list. Since I have no idea what the rest of your application looks like, it would be difficult for me to walk you through that.

    While it's NOT recommended, one way to deal with this problem would be to keep a reference to the parent clip of r1i1 and use that instead of parent to access it. Failing that, you should be able to find a more round-about way of accessing r1i1 (via root, perhaps).