你调用的对象是空的

时间:2022-03-05 14:31:13

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..

我最终将Object引用设置为一个对象的实例(在上面的代码中/////之间..

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

我试过创建一个Cell的单个实例,它工作正常。

4 个解决方案

#1


12  

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). 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

情况是,对于每个具有0 <= i 的i,我们有array>

HomeArray = new Cell [MAXCOL, MAXROW]; 

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:

你所做的就是建立一个对Cells的引用数组,但是你从未将这些引用分配给Cell的实例。也就是说,你告诉编译器“给我一个可以保存对Cells的引用的数组”,但你没有告诉编译器“给我一个可以保存对Cells的引用的数组,并将每个引用分配给一个新的Cell实例。 “因此,编译器会将这些引用的初始值设置为null。因此,您需要初始化HomeArray:

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

#2


4  

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

#3


3  

Arrays are initialised to be empty - the Null reference is because HomeArray[i,j] is null, not because HomeArray[i,j].currentCell is null.

数组初始化为空 - Null引用是因为HomeArray [i,j]为null,而不是因为HomeArray [i,j] .currentCell为null。

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.

更新:如果你有一个声明,其中有几个不同的东西可以为null,那么我通常将它分成多行,以便更容易分辨什么是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. If you split that statement up however:

HomeArray [i,j]或HomeArray [i,j] .currentCell可能都是null并触发NullReferenceException - 无法判断它是异常的。但是,如果您将该语句拆分为:

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,则在第二行得到它。

#4


0  

You are getting the exception because you are not assigning an instance of Cell to any of the slots of your matrices.

您正在获取异常,因为您没有将Cell的实例分配给矩阵的任何插槽。

#1


12  

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). 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

情况是,对于每个具有0 <= i 的i,我们有array>

HomeArray = new Cell [MAXCOL, MAXROW]; 

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:

你所做的就是建立一个对Cells的引用数组,但是你从未将这些引用分配给Cell的实例。也就是说,你告诉编译器“给我一个可以保存对Cells的引用的数组”,但你没有告诉编译器“给我一个可以保存对Cells的引用的数组,并将每个引用分配给一个新的Cell实例。 “因此,编译器会将这些引用的初始值设置为null。因此,您需要初始化HomeArray:

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

#2


4  

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

#3


3  

Arrays are initialised to be empty - the Null reference is because HomeArray[i,j] is null, not because HomeArray[i,j].currentCell is null.

数组初始化为空 - Null引用是因为HomeArray [i,j]为null,而不是因为HomeArray [i,j] .currentCell为null。

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.

更新:如果你有一个声明,其中有几个不同的东西可以为null,那么我通常将它分成多行,以便更容易分辨什么是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. If you split that statement up however:

HomeArray [i,j]或HomeArray [i,j] .currentCell可能都是null并触发NullReferenceException - 无法判断它是异常的。但是,如果您将该语句拆分为:

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,则在第二行得到它。

#4


0  

You are getting the exception because you are not assigning an instance of Cell to any of the slots of your matrices.

您正在获取异常,因为您没有将Cell的实例分配给矩阵的任何插槽。