
[英]C# Generic List.Any() throws System.NullReferenceException
[英]'System.NullReferenceException' while defining an object XNA C#
我最近一直在尝试制作游戏,并决定从头开始。 但是,我很快遇到了一个错误,这在我的游戏的上一期中不是问题。 我和我的朋友仔细研究了可能导致错误的几种方法,但我们一直未能找到。
显然,当我尝试使用其他类(UI.UILoader)加载对象时,它会以某种方式产生错误:主类如下:
namespace WindowsGame1.Classes
{
public static Game1 instance;
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public MouseState mouseState;
public KeyboardState keyState;
public SpriteFont debugFont;
Instance inst;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
debugFont = Content.Load<SpriteFont>("debugFont");
inst.Load();
UI.UILoader.Load();
// TODO: use this.Content to load your game content here
}
UILoader类可以在下面尝试使用Texture2D加载两个纹理的地方看到。 但是,当它尝试加载第一个纹理时,出现“ System.NullReferenceException”错误。
namespace WindowsGame1.Classes.UI
{
public static class UILoader
{
public static Texture2D cursorTexture;
public static Texture2D buttonTexture;
public static void Load()
{
cursorTexture = Game1.instance.Content.Load<Texture2D>("Interface/Cursor");
buttonTexture = Game1.instance.Content.Load<Texture2D>("Interface/Cursor");
}
}
}
我认为使用不同的类来加载可能会有所帮助(我发现这很奇怪),因此我尝试将其全部移动到主类中的加载内容类,但是,它没有消除错误,而是触发了在加载会话期间,它现在在我尝试使用该对象时触发(在该对象应被赋予一个值且因此不为null之后)
我不认为问题出在我尝试使用的图片上,因为我已经将其添加到了我正在使用的文件夹中(我会发布图片,但我的信誉不高)
如果有人知道导致此错误的原因,我很高兴在这里提出您的想法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.