【问题标题】:Cannot access a property or method of a null object reference, when accessing a text field on stage访问舞台上的文本字段时,无法访问空对象引用的属性或方法
【发布时间】:2014-08-17 05:00:40
【问题描述】:

我正在尝试在舞台上填充动态文本字段,但出现上述错误。我试图访问的文本字段存在于第 180 帧的舞台上。这个类扩展了 SimpleButton,它是一个超类,我有 5 个子类扩展了这个类。

我还尝试创建一个新文本而不是访问现有文本,但我遇到了同样的错误。

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);
                }


        }


    }

}

在另一个类中调用函数 getRecipe 只是为了填充数组配方并且它正在工作。 它让我得到以下输出:

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()

我的子类看起来像这样:

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);
        }
    }

}

提前致谢。任何帮助将不胜感激

【问题讨论】:

    标签: actionscript-3 class dynamic-text


    【解决方案1】:

    您的评论表明,当您尝试从扩展 TasteSweet 访问 r1i1 时遇到错误:

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

    问题是在执行该行代码之前,您正在离开Sweet 所在的当前帧:

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

    我的假设是 Sweet 的实例在场景 2 的第 180 帧的显示列表中不存在,在这种情况下,parent 为空。所以错误实际上是引用父级,而不是r1i1

    有很多方法可以解决这个问题。最重要的是不使用场景/帧来控制显示列表。由于我不知道您的应用程序的其余部分是什么样的,因此我很难引导您完成。

    虽然不建议这样做,但处理此问题的一种方法是保留对 r1i1 的父剪辑的引用,并使用它而不是 parent 来访问它。如果做不到这一点,您应该能够找到一种更全面的方式来访问r1i1(也许是通过root)。

    【讨论】:

    • 非常感谢,但为什么不推荐保留对 r1i1 父剪辑的引用并使用它而不是父剪辑?
    • 因为它在您的 Sweet 类和应用程序的其余部分之间创建了一个紧密耦合的环境。
    猜你喜欢
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 2012-12-29
    相关资源
    最近更新 更多