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?