Moynul
Moynul

Reputation: 635

Program crashes due to text being null. [As3]

[Fault] exception, information=TypeError: Error #2007: Parameter text must be non-null.

Hello guys, I get this error when I restart my program.

In the main class I have these variables

    //random
    private var colourArray:Array = new Array("Red","Yellow","Green");
    private var len:int = colourArray.length - 1;
    private var rand:int;
    private var randomItem:String;      
    private var myText:TextField;

then I have a loop that chest only the collision function

    private function Collision():void 
    {
        if (randomItem == ("Red"))
        {
            if (player.hitTestObject(red))
            {
                changeColor();
                red.removeMe();
            }
        }
        else
        if (randomItem == ("Yellow"))
        {
            if (player.hitTestObject(yellow))
            {
                changeColor();
                yellow.removeMe();
            }               

        }
        else
        if (randomItem == ("Green"))
        {
            if (player.hitTestObject(green))
            {
                changeColor();
                green.removeMe();
            }               
        }
    }

    private function changeColor():void 
    {
        var item:String = colourArray[rand];
        colourArray[rand] = colourArray[len];
        colourArray[len] = item;
        len--;
        randomItem = colourArray[rand];
        myText.text = randomItem;

        if (len < 0) 
        { 
            removeChild(player);
            removeChild(myText);

            colourArray.push("Red");
            removeEventListener(Event.ENTER_FRAME, gameLoop)
            addEventListener(Event.ENTER_FRAME, checkClicks)
            gotoAndStop(1);
        }

    }

When I try and restart the program it doesn't like ;

            randomItem = colourArray[rand];

For some reason.

I have repopulated the array, but I do not understand.

Thank you.

If you need more information please do ask.

Upvotes: 0

Views: 29

Answers (1)

Vesper
Vesper

Reputation: 18747

You have to store colourArray[rand] elsewhere prior to altering the array. You are lowering its length, then you query the element out of the array, so if your rand points to the last element, it gets kicked out of the array and replaced by NULL, and then you query the array again - the program says hello NULL, I panic. You already store that value in item variable, but don't use it for some weird reason. A fix is replace the randomItem = colourArray[rand]; with randomItem = item;

Upvotes: 1

Related Questions