2D Array System.NullReferenceException

Hello. In the class, map, there is a 2D array whose length is defined by rows and coluoms. In Map’s constructor, the array is filled with the string “Empty”.

Then in the other function of the Map class, LocationIsOnMap, it receives a index and performs a check on the object that is in that index in the array. In that if statement I get a System.NullReferenceException when I try to do map[loc.x,loc.y]. I also get this error whenever I try and use map in anyway in that function. I’m confident that LocationIsOnMap is within map’s scope so I don’t know whats wrong here.

    public class Map
    {
        protected int rows;
        protected int coluoms;
        protected string[,] map;
        // Constructor
        public Map ()
        {
            rows = 5;
            coluoms = 5;
            // A 2D array for the map with rows and coluoms for its dimensions
            string[,] map = new string [rows,coluoms];

            for (int i = 0; i < rows; i++)
            {
                for (int k = 0; k < coluoms; k++)
                {
                    map[i,k] = "Empty";
                }
            }

            for (int i = 0; i < rows; i++)
            {
                for (int k = 0; k < coluoms; k++)
                {
                    /*Console.WriteLine("map[i,k] = " +  map[i,k]);
                    Console.WriteLine ("i = " + i);
                    Console.WriteLine ("k = " + k);*/
                }
            }
            //Console.WriteLine(map.GetLength(0));
        }


        // Returns a bool stating if the given location is on the map
        public bool LocationIsOnMap(Vector loc)
        {
            bool retBool = false;

            if (loc.x <= coluoms & loc.y <= rows)
            {
                if (map[loc.x,loc.y] == "Empty")
                {
                    retBool = true;
                }
            }
            return retBool;
        }
    }
    // End of class
}

line 40 uses & instead of &&. Change that and see if that helps?

Additionally, your map building builds the rows into the columns and the columns into the rows. If your map isn’t square, this will cause confusion and IndexOutOfBounds exceptions.

That didn’t fix it. If I do map.GetType() in the that function, it gives me that same error.

What happens if you uncomment out line 31? Is that called before the NRE? From the code provided, there should never be a case where map is null as it is instantiated within the provided constructor.

Uncommenting 31 didn’t cause any problems. Same with uncommenting 26-28. What does this error mean exactly?

You’re never actually instantiating your 2D array. This line is creating a new map variable local to your constructor’s scope:

string[,] map = new string [rows,coluoms];

Delete the string[,] portion to avoid this:

map = new string[rows, coluoms];
2 Likes

did you mean to use the Vector class/struct from the c# core libraries rather than unity’s Vector2/3/4 classes?

Thats fixes it, thanks to all.

I actully couldn’t figure out how to include the namespace C#'s vector was in so I made my own struct that has 2 public fields, x and y. Its good enough for my uses. Whats the difference between C#'s vector and Unity’s vector and how do I include their namespaces?

Rarg! How did I not see that!