Search code examples
actionscript-3flash

AS3 - Error #1009: Cannot access a property or method of a null object reference


I have no idea what is causing the error. I'm sure its very obvious. (See comments below).

package src.main{

    import src.tilespack.Tile;

    public class Main{

        public var tile:Tile;

        public function Main{
            var t:Tile = Tile.tiles[0]; //Error here

            trace(Tile); //This will also cause an error
        }

Tile:

package src.tilespack{

    public static var tiles:Array = [];

    public static var floorTile:Tile = new FloorTile(0); //Causes an error in FloorTile Class

    public var bitmapData:BitmapData;

    public function Tile(bitmapData:BitmapData, ID:int)
    {
        this.ID = ID;
        tiles[ID] = this;
        this.bitmapData = bitmapData;
    }

}

FloorTile:

package src.tilespack{
    import src.gfx.Assets;
    import src.tilespack.Tile;

    public class FloorTile extends Tile{ //Error here

        public function FloorTile(ID:int){
            super(Assets.floorTileData, ID);
        }
    }
}

Error #1009: Cannot access a property or method of a null object reference.


Solution

  • I've noticed few problems in your code:

    1) The file Tile doesn't have a definition of class. Probably, this is just a typo, but anyway, this file should look something like:

    package src.tilespack
    {
        public class Tile
        {
            public static var tiles:Array = [];
    
            public static var floorTile:Tile = new FloorTile(0); //Causes an error in FloorTile Class
    
            public var ID:int;
            public var bitmapData:BitmapData;
    
            public function Tile(bitmapData:BitmapData, ID:int)
            {
                this.ID = ID;
                tiles[ID] = this;
                this.bitmapData = bitmapData;
            }
        }
    }
    

    2) Your code has something like "recursive linkage" (sorry, I don't know the official term). Your class Tile has the static variable floorTile, and it tries to create an instance of the FloorTile class, which itself extends the class Tile.

    So we have a situation, when the class Tile tries to use the class FloorTile (because static variables should be instantiated during the first class use), and the class FloorTile tries to use the class Tile. And it's a problem.

    You may either remove static variable of the type FloorTile or change code of the class Tile to prevent usage of the class FloorTile before the class Tile is prepared to work. Here an example of the second approach:

    package src.tilespack
    {
        import flash.display.BitmapData;
    
        public class Tile
        {
            public static var tiles:Array = [];
    
            private static var _floorTile:Tile;
            public static function get floorTile():Tile
            {
                if (!Tile._floorTile)
                {
                    Tile._floorTile = new FloorTile(0);
                }
    
                return Tile._floorTile;
            }
    
            public var ID:int;
            public var bitmapData:BitmapData;
    
            public function Tile(bitmapData:BitmapData, ID:int)
            {
                this.ID = ID;
                tiles[ID] = this;
                this.bitmapData = bitmapData;
            }
        }
    }