你调用的对象是空的
我有一个类 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; }
}
然后尝试在以下类中使用它:
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());
///////////////////////////////////////////////////////
}
}
}
}
我最终得到的对象引用未设置为对象的实例(在上面代码中的 ///// 之间。
我尝试创建Cell 的单个实例并且工作正常。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
实例化数组时,数组中的项目会收到该类型的默认值。因此,
对于每个
i
且0 <= i << length
我们有array[i] = default(T)
。因此,对于引用类型array[i]
将为null
。这就是您看到NullReferenceException
的原因。在您的情况下,Cell
是一种引用类型,因此您所做的就是建立一个对
Cell
的引用数组,但您从未将这些引用分配给的实例代码>单元格。也就是说,您告诉编译器“给我一个可以保存对 Cell 的引用的数组”,但您没有告诉编译器“给我一个可以保存对 Cell 的引用的数组” code>s 并将每个引用分配给
Cell
的新实例。”因此,编译器会将这些引用的初始值设置为null
。因此,您需要初始化HomeArray
:When you instantiate an array, the items in the array receive the default value for that type. Thus for
it is the case that for every
i
with0 <= i < length
we havearray[i] = default(T)
. Thus, for reference typesarray[i]
will benull
. This is why you are seeing theNullReferenceException
. In your caseCell
is a reference type so since you haveand all you have done is establish an array of references to
Cell
s but you never assigned those references to instances ofCell
. That is, you told the compiler "give me an array that can hold references toCell
s" but you did not tell the compiler "give me an array that can hold references toCell
s and assign each of those references to a new instance ofCell
." Thus, the compiler will set the initial value of those references tonull
. Therefore you need to initialize theHomeArray
:您需要初始化数组中的单元格。
You need to initialize the Cells in your arrays.
数组初始化为空 - Null 引用是因为
HomeArray[i,j]
为 null,而不是因为HomeArray[i,j].currentCell
为 null。更新:如果你有一个语句,其中几个不同的东西可能为空,那么我通常将其分成多行,以便更容易分辨什么为空。
例如,在您的情况下:
HomeArray[i,j]
或HomeArray[i,j].currentCell
可能为 null 并触发 NullReferenceException - 没有办法来判断它是来自异常。但是,如果您将该语句分开:在这种情况下,如果
HomeArray[i,j]
为 null,那么您将在第一行得到 NullReferenceException,而如果cell
为 null,则您将得到 NullReferenceException。把它放在第二行。Arrays are initialised to be empty - the Null reference is because
HomeArray[i,j]
is null, not becauseHomeArray[i,j].currentCell
is 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.
For example, in your case:
Either
HomeArray[i,j]
orHomeArray[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:In this case if
HomeArray[i,j]
is null then you get your NullReferenceException on the first, line whereas ifcell
is null you get it on the second line.您收到异常是因为您没有将 Cell 实例分配给矩阵的任何槽。
You are getting the exception because you are not assigning an instance of Cell to any of the slots of your matrices.