1
votes

I have basically been trying to create a chess game, and it has some basic functionality. I have encountered some runtime errors that I can't seem to fix. I've tried searching around for solutions, and I have found several, but none of them seem to work for me.

I then created a new flash project to see if the error still occurs, just to make sure that it's not another part of my program that is causing the issue.

The stage has a pentagon object (a MovieClip) on it, which has an instance name of PentaGray and a class of Gray. There is an intermediary class called PentaClass.

The Main (document) class:

import flash.display.MovieClip;
import PentaClass;
import Gray;

public class Main extends MovieClip
{
    // public var Natsu:String;

    public function Main()
    {
        FairyTail();
    }

    public function FairyTail()
    {
        trace("PentaGray = " + PentaGray);
            trace("PentaGray's name is " + PentaGray.name);
            PentaGray.TraceThis();
    }
}

PentaClass class:

public class PentaClass extends Main
{

    public function PentaClass()
    {
    }

    public function TraceThis():void
    {
        trace("Fairy Tail :D");
    }
}

Gray class:

public class Gray extends PentaClass
{
    public function Gray()
    {
        // TraceThis();
    }
}

Running the program like this causes this to be printed to the output window:

PentaGray = null
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main/FairyTail()
at Main()
at PentaClass()
at Gray()
at flash.display::Sprite/constructChildren()
at flash.display::Sprite()
at flash.display::MovieClip()
at Main()
PentaGray = null
TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Main/FairyTail()
at Main()

If I change the code in the Main class to:

if (PentaGray)
{
    trace("PentaGray's name is " + PentaGray.name);
    PentaGray.TraceThis();
}

Then this is output:

PentaGray = null
PentaGray = [object Gray]
PentaGray's name is PentaGray
Fairy Tail :D

Also, if I run the function from Gray by uncommenting the statement in its constructor, it works properly, but for my game, I need to run a subclass function from the document class.

What I would like to to know is, why is PentaGray null when the program starts? I thought that the objects that are placed on the stage using the IDE are initialised before any written code runs, but this doesn't seem to be the case.

I've tried using event listeners (ENTER_FRAME and a Timer) but they still didn't solve the issue.

How can I change the program so that the object isn't null when certain code, e.g. the FairyTail() function, is executed? Have I not written my code properly or is this an issue with Flash/ActionScript itself?

2

2 Answers

0
votes

By putting the call to FairyTail() in the Main class in the Main function, you have put that in the 'constructor' for the main object. The constructor of the Main object is the very first thing that has to run, because nothing can be created until the main object is created. And when the main object is created, the constructor is called first.

If you want something to run right when PentaGray is created, put it in the constructor of PentaClass:

public class PentaClass extends Main
{

    public function PentaClass()
    {
        Main.FairyTail();
    }

}

Or, if you just want it to happen at the start, you can just put it after the class declaration in the main file.

#main document

import flash.display.MovieClip;
import PentaClass;
import Gray;

public class Main extends MovieClip {
    //...
}

Main.FairyTail();

By the way, it doesn't make sense to me that the PentaClass extends the Main class. Why don't you just make another class for it to extend?

0
votes

Off course, your stage is not ready. Use the bootstrap done below in your main class.

    public function Main():void 
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init(e:Event = null):void 
    {
        removeEventListener(Event.ADDED_TO_STAGE, init);
        FairyTail();
    }

Cheers