你调用的对象是空的 [英] Object reference not set to an instance of an object

查看:159
本文介绍了你调用的对象是空的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一类细胞:

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();
    } 
}

这篇关于你调用的对象是空的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆