Object reference not set to an instance of an object

前端 未结 4 1263
滥情空心
滥情空心 2021-01-18 19:45

I have a class Cell:

public class Cell
{
    public enum cellState
    {
        WATER,
        SCAN,
        SHIPUNIT,
        SHOT,
        HIT
    }

             


        
4条回答
  •  滥情空心
    2021-01-18 20:09

    You need to initialize the Cells in your arrays.

    public NietzscheBattleshipsGameModel()
    {
        HomeArray = new Cell[MAXCOL, MAXROW];
        AwayArray = new Cell[MAXCOL, MAXROW];
    
        for (int i = 0; i < MAXROW; i++)
        {
            for (int j = 0; j < MAXCOL; j++)
            {
                HomeArray[i,j] = new Cell();
                AwayArray[i,j] = new Cell();
            }
        }
    }
    

提交回复
热议问题