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
}