0

Essentially I am attempting to make my own game of life in C# with a Map class whose instances contain an array of Life(Cell) objects. However in my program attempting to check: if (lifemap.obj[y, x].isAlive == true) Where lifemap is an object containing an array of cell like objects throws a System.NullReferenceException: 'Object reference not set to an instance of an object.' in visual studio. I set that line to a breakpoint and looked in the debugger to see that my "obj" variable containing an array of Life instances has a value of null. I can't see how this could be so any help would be appreciated, my code for the constructor for the Map object is as follows:

public Map(int rows,int columns)
{
    this.columns = columns;
    this.rows = rows;
    Life[,] obj = new Life[rows, columns];
    Random rnd = new Random();
    for (int x = 0; x < columns; ++x)
        for (int y = 0; y < rows; ++y)
        {
            if (rnd.Next(100) < 50) obj[y, x] = new Life(false);
            else
            {
                obj[y, x] = new Life(true);
                ++alivecellscount;
            }
        }
}
addey
  • 1
  • 1
  • [Can't reproduce](https://rextester.com/KLGUJ89982). Clean and Rebuild your solution. – ProgrammingLlama Sep 19 '20 at 16:24
  • How do you access `lifemap.obj` when `obj` is local, declared inside `public Map()` (`Life[,] obj = new Life[rows, columns];`)? This method has no return type. You should post real code. You should declare your `Random rnd` outside that method. – Jimi Sep 19 '20 at 16:29

0 Answers0