简体   繁体   English

为什么这一行抛出异常?

[英]Why does this line throw an exception?

I have been troubled by this for a while now, this is the code I have: 我已经困扰了一段时间了,这是我的代码:

TILArray = new string[Width, Height];
int t = 0;
TILArray[t, t] = "";
TILArray[t, t] = "";
for (int x = 0; x < Width; x++)
{
    for (int y = 0; y < Height; y++)
    {
        if (TILList[x][y] != null)
        {
            TILArray[0, 0] = TILList[x][y];
            Tiles[x, y] = Content.Load<Tile>(TILList[x][y]);
        }// This line throws the excetion
    }
}

The line that throws the exception is actually the right curly brace marked above. 抛出异常的行实际上是上面标记的右大括号。

An unhandled exception of type 'System.NullReferenceException' occurred in TileEngine.dll TileEngine.dll中出现未处理的“System.NullReferenceException”类型异常
Additional information: Object reference not set to an instance of an object. 附加信息:未将对象引用设置为对象的实例。

My guess is that it's Content that is null and you're calling the method before the game's LoadContent method is called, or before you set up Content . 我的猜测是它的Content为null,你在调用游戏的LoadContent方法之前或者在设置Content之前调用方法。

Ensure you're setting the Content member correctly. 确保您正确设置了Content成员。 This should be done for you in the default game template. 这应该在默认游戏模板中为您完成。 Also, ensure you're loading content after (or within) the game's LoadContent method. 此外,确保您在游戏的LoadContent方法之后(或之内)加载内容。

当我设置固定它的宽度和高度时,我需要重新初始化Tiles数组

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

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