Skip navigation
Currently Being Moderated

null object reference starling

Apr 22, 2012 9:13 AM

This function is called within a for loop to create 4 buttons ... select the right frame and display it.

Problem is I get a null object reference in addChild... line.

I'm using starling and I don't know how to get rid of this error. Can you help ?

 

private function newButton(i:int):void

                    {

                              var newBtn:_menuButton = new _menuButton();

                              newBtn.gotoAndStop(i + 1);

                              menuGroup[i] = newBtn;

                              addChild(newBtn as DisplayObject); //starling.display.DisplayObject

 

                              newBtn.addEventListener(TouchEvent.TOUCH, onTouch, false, 0, true);

                    }

 

Also the code on Starling :

 

        public function addChild(child:DisplayObject):void

        {

            addChildAt(child, numChildren);

        }

       

        /** Adds a child to the container at a certain index. */

        public function addChildAt(child:DisplayObject, index:int):void

        {

            if (index >= 0 && index <= numChildren)

            {

                child.removeFromParent(); //I get error in this line primarly.

                mChildren.splice(index, 0, child);

                child.setParent(this);               

                child.dispatchEvent(new Event(Event.ADDED, true));

                if (stage) child.dispatchEventOnChildren(new Event(Event.ADDED_TO_STAGE));

            }

            else

            {

                throw new RangeError("Invalid child index");

            }

        }

 
Replies

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points