2011-10-12 37 views
0

我知道以前有人问过这个问题,但似乎他们的解决方案对我不起作用,或者我做错了什么。未将对象引用设置为对象的实例#100

public class Sprite 
{ 
    private Game m_game; 
    private SpriteBatch m_spriteBatch; 
    private string m_filename; 
    private Texture2D m_texture; 

    public Sprite(Game game, SpriteBatch spriteBatch, GraphicsDevice graphicsDevice) 
    { 
     m_game = game; 
     m_spriteBatch = spriteBatch; 
     m_texture = new Texture2D(graphicsDevice, graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height); 
    } 

    public void LoadSprite(string filename) 
    { 
     m_texture = m_game.Content.Load<Texture2D>(filename); 
    } 
} 

当我传递“tree”作为文件名时,在LoadSprite中产生错误。 m_texture不为null,因为(试图)在构造函数中初始化它。 对主循环使用与Content.Load相同的调用,但我想将其移入Sprite类。

treeTexture = Content.Load<Texture2D>("tree"); 

这在主循环中正常工作,因此它显示“树”文件存在。

任何人都可以看到我做错了什么?

+3

m_game或m_game.Content为空吗? – Joey

+1

那么你的IDE应该很容易为你提供哪些对象是NULL。我们不知道... – RvdK

+0

可能的重复[什么是.NET中的NullReferenceException?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net) –

回答

0

m_game或m_game.Content可能为空。

+0

m_game为空,因为在调用中传递给它的“游戏”为null。我必须把:game = this;在主类的构造函数中。 –

相关问题