0

I'm a beginner to c# and I keep getting a 'System.NullReferenceException' error. I've looked everywhere but I can't seem to find a useful solution. I simplified the code below so that it would be more clear.

namespace tile_test
{
    public class Game1 : Game
    {
        public static float bottomWorld = 38400f;
        public static float rightWorld = 134400f;
        public static int maxTilesX = (int)rightWorld / 16 + 1;
        public static int maxTilesY = (int)bottomWorld / 16 + 1;


        public Game1()
        {
            Tile[,] tile = new Tile[maxTilesX, maxTilesY];
            int x = 1;
            int y = 1;
            tile[x, y].active = false; //Error on this line.
        }
    }
}

The Tile-class is shown below

namespace tile_test
{
    public class Tile
    {
        public bool active;
    }
}

Could anyone help me out?

3 Answers 3

2

You have declared an array to store your Tile objects for the dimensions needed, but every single slot of this array is NULL, you can't reference a NULL trying to assign the property active

Tile[,] tile = new Tile[maxTilesX, maxTilesY];
int x = 1;
int y = 1;
tile[x, y] = new Tile() {active=false};

and you need a code like this for every Tile that you plan to store in your array

Sign up to request clarification or add additional context in comments.

Comments

2

First initialize tile[x, y]

tile[x, y] = new Tile();
tile[x, y].active = false;

To Initialize all the element of your array you can create a utility method

 T[,] Create2DimArray<T>(int len1,int len2) where T: new()
    {
        T[,] arr = new T[len1, len2];
        for (int i = 0; i < len1; i++)
        {
            for (int j = 0; j < len2; j++)
            {
                arr[i, j] = new T();
            }
        }
        return arr;
    }

and use it as

Tile[,] tile = Create2DimArray<Tile>(maxTilesX, maxTilesY);

Comments

0

A System.NullReferenceException is thrown when you try and perform an operation of an object which doesn't exist (has a value of null) - in this case your Tile at position 1,1 in the array doesn't exist yet, so the array stores the value null in-place of a proper reference.

You need to instantiate all the items in your Tiles array before you try and use them. when you create the array they all have the default null value because there is no object on the heap to reference yet at.

This is simply done after you create the array if you want to create all the tiles at once:

for (int i = 0; i < maxTilesX; i++)
{ // loop through "rows" of tiles
    for (int j = 0; j < maxTilesY; j++)
    { // loop through corresponding "column" of tiles
        tile[i, j] = new Tile(); // create a Tile for the array to reference
        tile[i, j].active = false; // some initialization
    }
}

Just so you know, C# uses Zero-Indexed arrays, so the first item in your array is in-fact tile[0, 0]: More about arrays on the MSDN C# Arrays Tutorial if you want to read more. Sorry if you already knew this!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.