你调用的对象是空的

由网友(一起闹一起疯一起哭一起玩)分享简介:我有一类细胞:public class Cell{public enum cellState{WATER,SCAN,SHIPUNIT,SHOT,HIT}public Cell(){currentCell = cellState.WATER;MessageBox.Show(currentCell.ToString()...

我有一类细胞:

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

    public Cell()
    {
        currentCell = cellState.WATER;
        MessageBox.Show(currentCell.ToString());
    }

    public cellState currentCell { get; set; }
}

然后我尝试使用它在下面的类:

I then try to use it in the following class:

public class NietzscheBattleshipsGameModel
{
    private byte MAXCOL = 10;
    private byte MAXROW = 10;

    public Cell[,] HomeArray;

    private Cell[,] AwayArray;

    public NietzscheBattleshipsGameModel()
    {
        HomeArray = new Cell [MAXCOL, MAXROW];

        AwayArray = new Cell [MAXCOL, MAXROW];
    }


    public string alphaCoords(Int32 x)
    {
        if (x < 0 || x > 9)
        {
            throw new ArgumentOutOfRangeException();
        }

        char alphaChar = (char)('A' + x);

        return alphaChar.ToString();
    }

    public void test()
    {
        for (int i = 0; i < 10; i++)
        {
            for (int j = 0; j < 10; j++)
            {

                // Object reference not set to an instance of an object.
                MessageBox.Show(HomeArray[i,j].currentCell.ToString());
                ///////////////////////////////////////////////////////

            }
        }
    }
}

我结束了在上述code不设置到对象的实例对象引用(/////之间。

I end up with the Object reference not set to an instance of an object (between the ///// in the above code..

我曾尝试创建细胞的一个实例,它工作正常。

I have tried creating a single instance of Cell and it works fine.

推荐答案

当你初始化数组,数组中的项目将收到该类型的默认值。因此,对于

When you instantiate an array, the items in the array receive the default value for that type. Thus for

T[] array = new T[length];

正是如此,每 0℃= I&LT;长度我们数组[我] =默认(T)。因此,对于引用类型数组[我] 。这就是为什么你看到的的NullReferenceException 。在你的情况细胞是引用类型,这样,因为你有

it is the case that for every i with 0 <= i < length we have array[i] = default(T). Thus, for reference types array[i] will be null. This is why you are seeing the NullReferenceException. In your case Cell is a reference type so since you have

HomeArray = new Cell [MAXCOL, MAXROW]; 

和你所做的就是建立引用数组细胞取值但你永远不分配这些引用细胞。也就是说,你告诉编译器给我,可容纳引用细胞的数组,但你并没有告诉编译器给我,可容纳引用数组细胞和那些引用赋值给细胞的新实例。因此,编译器将设置这些引用了的初始值。因此,你需要初始化 HomeArray

and all you have done is establish an array of references to Cells but you never assigned those references to instances of Cell. That is, you told the compiler "give me an array that can hold references to Cells" but you did not tell the compiler "give me an array that can hold references to Cells and assign each of those references to a new instance of Cell." Thus, the compiler will set the initial value of those references to null. Therefore you need to initialize the HomeArray:

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