Search code examples
c#xna-4.0

C# Getting System.NullRefrenceException from Loading map from 2array


When i try to start i got System.NullRefrenceException no idéa why my code is:

Tile.cs:

class Tile : Sprite
{
    public bool IsBlocked { get; set; }

    public Tile(Texture2D texture, Vector2 position, SpriteBatch batch, bool isBlocked)
        : base(texture, position, batch)
    {
        IsBlocked = isBlocked;
    }

    public override void Draw()
    {
        SpriteBatch.Draw(Texture, Position, Color.White);
    }
}

this is my base code to spawn my tiles

and this is my map loader

MapLoader.cs

class MapLoader
{
    List<Texture2D> tileList = new List<Texture2D>();
    public Tile[,] Tiles { get; set; }
    public Texture2D TileTexture { get; set; }
    public SpriteBatch SpriteBatch { get; set; }
    static int TileWidth = 64;
    static int TileHeight = 64;
    int mapWidth;
    int mapHeight;

    private int[,] map = 
    {
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, 
        { 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0,}, 
        { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0,}, 
        { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,}, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}, 
        { 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0,}, 
        { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0,}, 
        { 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 2, 1, 1,}, 
        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,}
   };

    public MapLoader(SpriteBatch spritebatch, Texture2D normaltile, Texture2D nulltile, Texture2D SharpTile, Texture2D MovingTile)
    {
        tileList.Add(nulltile);
        tileList.Add(normaltile);
        tileList.Add(SharpTile);
        tileList.Add(MovingTile);
        mapWidth = map.GetLength(1);
        mapHeight = map.GetLength(0);
        for (int x = 0; x < mapWidth; x++)
        {
            for (int y = 0; y < mapHeight; y++)
            {
                    Vector2 tilePosition =
                        new Vector2(TileWidth * tileList[map[x, y]].Width, TileHeight * tileList[map[x, y]].Height);
                    Tiles[x, y] =
                        new Tile(tileList[map[x, y], tilePosition, spritebatch, map[x, y] != 0);
            }
        }
    }
    public void Draw()
    {
        foreach (var tile in Tiles)
        {
            tile.Draw();
        }
    }
}

the error happends in this line of code

Tiles[x, y] = new Tile(tileList[map[x, y], tilePosition, spritebatch, map[x, y] != 0);

and what of i know i fill it.


Solution

  • If you get a NullReferenceException you access a variable whose value is null. Possible candidates: all variables in the line above, that might be accessed within the constructor.

    Obviously in the constructor of MapLoader you don't create the array Tile[,] Tiles. Just declaring a property does not create the actual value on the heap.

    Review the array syntax rules of C#!

    Update your constructor and add this line `Tiles = new Tile[mapWidth, mapHeight]; before the first for-loop and you'll be fine!