[英]Object reference not set to an instance of an object - C#
I'm a beginner to c# and I keep getting a 'System.NullReferenceException' error. 我是C#的初学者,并且不断收到“ System.NullReferenceException”错误。 I've looked everywhere but I can't seem to find a useful solution.
我到处都看过,但似乎找不到有用的解决方案。 I simplified the code below so that it would be more clear.
我简化了下面的代码,以使其更加清晰。
namespace tile_test
{
public class Game1 : Game
{
public static float bottomWorld = 38400f;
public static float rightWorld = 134400f;
public static int maxTilesX = (int)rightWorld / 16 + 1;
public static int maxTilesY = (int)bottomWorld / 16 + 1;
public Game1()
{
Tile[,] tile = new Tile[maxTilesX, maxTilesY];
int x = 1;
int y = 1;
tile[x, y].active = false; //Error on this line.
}
}
}
The Tile-class is shown below Tile类如下所示
namespace tile_test
{
public class Tile
{
public bool active;
}
}
Could anyone help me out? 有人可以帮我吗?
You have declared an array to store your Tile objects for the dimensions needed, but every single slot of this array is NULL, you can't reference a NULL trying to assign the property active 您已经声明了一个数组来存储需要的尺寸的Tile对象,但是此数组的每个插槽均为NULL,因此您无法引用NULL尝试将属性分配为active
Tile[,] tile = new Tile[maxTilesX, maxTilesY];
int x = 1;
int y = 1;
tile[x, y] = new Tile() {active=false};
and you need a code like this for every Tile that you plan to store in your array 对于计划存储在数组中的每个Tile,都需要这样的代码
First initialize tile[x, y]
首先初始化
tile[x, y]
tile[x, y] = new Tile();
tile[x, y].active = false;
To Initialize all the element of your array you can create a utility method 要初始化数组的所有元素,您可以创建一个实用程序方法
T[,] Create2DimArray<T>(int len1,int len2) where T: new()
{
T[,] arr = new T[len1, len2];
for (int i = 0; i < len1; i++)
{
for (int j = 0; j < len2; j++)
{
arr[i, j] = new T();
}
}
return arr;
}
and use it as 并用作
Tile[,] tile = Create2DimArray<Tile>(maxTilesX, maxTilesY);
A System.NullReferenceException
is thrown when you try and perform an operation of an object which doesn't exist (has a value of null
) - in this case your Tile at position 1,1 in the array doesn't exist yet, so the array stores the value null
in-place of a proper reference. 尝试执行不存在的对象的操作(具有
null
)时,将引发System.NullReferenceException
在这种情况下,数组中位置1,1的Tile尚不存在,因此数组在适当的引用位置存储null
值。
You need to instantiate all the items in your Tiles array before you try and use them. 您需要实例化Tiles数组中的所有项目,然后才能尝试使用它们。 when you create the array they all have the default
null
value because there is no object on the heap to reference yet at. 创建数组时,它们都具有默认的
null
值,因为堆上尚没有要引用的对象。
This is simply done after you create the array if you want to create all the tiles at once: 如果要一次创建所有切片,只需在创建阵列后完成:
for (int i = 0; i < maxTilesX; i++)
{ // loop through "rows" of tiles
for (int j = 0; j < maxTilesY; j++)
{ // loop through corresponding "column" of tiles
tile[i, j] = new Tile(); // create a Tile for the array to reference
tile[i, j].active = false; // some initialization
}
}
Just so you know, C# uses Zero-Indexed arrays, so the first item in your array is in-fact tile[0, 0]
: More about arrays on the MSDN C# Arrays Tutorial if you want to read more. 众所周知,C#使用零索引数组,因此数组中的第一项实际上是
tile[0, 0]
:如果您想了解更多信息tile[0, 0]
参见MSDN C#数组教程中有关数组的更多信息。 Sorry if you already knew this! 抱歉,如果您已经知道这一点!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.