简体   繁体   English

你调用的对象是空的

[英]Object reference not set to an instance of an object

I have a class Cell: 我有一堂课:

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

            }
        }
    }
}

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. 我尝试创建一个Cell实例,并且效果很好。

When you instantiate an array, the items in the array receive the default value for that type. 实例化数组时,数组中的项目会收到该类型的默认值。 Thus for 因此对于

T[] array = new T[length];

it is the case that for every i with 0 <= i < length we have array[i] = default(T) . 对于每个0 <= i < length0 <= i < length i ,我们都有array[i] = default(T) Thus, for reference types array[i] will be null . 因此,对于引用类型, array[i]将为null This is why you are seeing the NullReferenceException . 这就是为什么您看到NullReferenceException In your case Cell is a reference type so since you have 在您的情况下, Cell是引用类型,因此既然

HomeArray = new Cell [MAXCOL, MAXROW]; 

and all you have done is establish an array of references to Cell s but you never assigned those references to instances of Cell . 您所做的只是建立对Cell的引用数组,但您从未将这些引用分配给Cell实例。 That is, you told the compiler "give me an array that can hold references to Cell s" but you did not tell the compiler "give me an array that can hold references to Cell s and assign each of those references to a new instance of Cell ." 也就是说,您告诉编译器“给我一个可以保存对Cell的引用的数组”,但您没有告诉编译器“给我一个可以保存对Cell的引用的数组,并将这些引用中的每一个分配给的新实例。 Cell 。” Thus, the compiler will set the initial value of those references to null . 因此,编译器会将这些引用的初始值设置为null Therefore you need to initialize the HomeArray : 因此,您需要初始化HomeArray

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

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

Arrays are initialised to be empty - the Null reference is because HomeArray[i,j] is null, not because HomeArray[i,j].currentCell is null. 数组初始化为空HomeArray[i,j]引用是因为HomeArray[i,j]为空,而不是因为HomeArray[i,j].currentCell为空。

UPDATE: If you have a statement where a couple of different things could be null, then I generally split that up into multiple lines to make it easier to tell what is null. 更新:如果您有一个声明,其中有两个不同的事物可能为空,那么我通常将其拆分为多行,以便于更容易分辨出什么为空。

For example, in your case: 例如,在您的情况下:

MessageBox.Show(HomeArray[i,j].currentCell.ToString());

Either HomeArray[i,j] or HomeArray[i,j].currentCell could potentially be null and trigger a NullReferenceException - there is no way to tell which it was from the exception. HomeArray[i,j]HomeArray[i,j].currentCell都可能为null并触发NullReferenceException-无法从异常中分辨出它是谁。 If you split that statement up however: 但是,如果拆分该语句:

Cell cell = HomeArray[i,j].currentCell;
MessageBox.Show(cell.ToString());

In this case if HomeArray[i,j] is null then you get your NullReferenceException on the first, line whereas if cell is null you get it on the second line. 在这种情况下,如果HomeArray[i,j]为null,则在第一行得到NullReferenceException,而如果cell为null,则在第二行得到它。

之所以会出现异常,是因为没有将Cell实例分配给矩阵的任何插槽。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM