2011-10-12 4 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가 null입니까? – 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가 아마도 null 일 수 있습니다.

+0

m_game은 호출시 전달 된 "game"이 null이므로 m_game이 null이었습니다. 나는 넣어야했다 : game = this; 메인 클래스의 생성자에서. –

관련 문제