2012-04-19 2 views
1

이제는 오류가 무엇을 의미하는지 모르겠지만 신의 삶에 대해이 문제의 원인이 될 수있는 것이 무엇인지 알 수 없습니다. 나는 대답을 찾고 많이 찾았지만, 나의 고뇌에 대한 해결책은 나를 벗어난다. 여러 번 저의 장애물을 극복하려했지만 실패했습니다. 지금은 당신이 평화 : 나의 뇌 나머지 도움이 될 수 있습니다, 깨달음을 기대 당신을 바라 볼 여기 XNA C# content.load <spritefont> 개체 참조가 개체의 인스턴스로 설정되지 않았습니다.

문제는 온다 : GAME1는 실행 Abstakt라는 내 수업과의 일을한다 : 필자가 제거

class Abstrakt 
{ 
    public ContentManager content; 
    public SpriteBatch spriteBatch; 
    public GraphicsDeviceManager graphics; 

    MenuComponent menuComponent; 
    StartGame startGame; 

    public string gameState = "Menu"; 

    public Abstrakt(SpriteBatch spriteBatch, ContentManager content, GraphicsDeviceManager graphics) 
    { 
     this.spriteBatch = spriteBatch; 
     this.content = content; 
     this.graphics = graphics; 
    } 

    public Abstrakt() { } 

    public virtual void Initialize() 
    { 
     menuComponent = new MenuComponent(); 
     startGame = new StartGame(); 

     menuComponent.Initialize(); 
     startGame.Initialize(); 
    } 

    public virtual void LoadContent() 
    { 
     menuComponent.LoadContent(); 
     startGame.LoadContent(); 
    } 
}   






class MenuComponent : Abstrakt 
{ 
    SpriteFont spriteFont; 

    public MenuComponent() { } 

    public override void LoadContent() 
    { 
     spriteFont = content.Load<SpriteFont>("GameFont"); <--Here the problem appears 
    } 
} 

중요하지 않은 코드이므로 쉽게 볼 수 있습니다. 문제는 다음과 같습니다. 개체 참조가 개체의 인스턴스로 설정되지 않았습니다.

도움과 조언을 주셔서 감사합니다.

답변

3

"콘텐츠"변수를 초기화하지 않는 것이 좋습니다.

당신이 예는 null로 동일 있도록 기본 매개 변수가없는 생성자와의 MenuComponent는, 내용이

+0

+1 ... .. 할당되지 않은,이 오류를 얻을 수있는 유일한 그럴듯한 방법이라고 생각하는 경우 설명 된 메시지. @ user1244948 : 디버거를 조금 더 사용하는 법을 배웁니다. 여기서 충돌이 발생하면 마우스를 변수 위에 놓아 현재 상태를 볼 수 있습니다. '콘텐츠 '위에 마우스를 올려 놓으면 그 자리에 문제가 나타 났을 것입니다. –

+0

고마워, 네, 그게 문제였습니다. 나는 그것이 값을 얻기 전에 Game1 클래스를 사용했다. 나는 다음과 같은 코드를 가진 다른 조각으로 같은 problme를 쳤지 만, pPosition = new Vector2 ((graphics.PreferredBackBufferWidth - pTexture.Width)/2, (graphics.PreferredBackBufferHeight - pTexture.Height)/2); 내 LoadContent 메서드에서 문제를 해결하려고 노력하고 있지만 ... 한 걸음 더 나아가도록 도와 주셔서 감사합니다. D – user1344948

+0

@ john mcdonald 조언을 따르고, 디버거를 사용해야하며, 외부 그래픽을 사용하지 마십시오. 게임 생성자 ... 대신 GraphicsDevice.Viewport.Width/Height를 사용하고 ... pTexture가 null이 아닌지 확인해야합니다.) – Blau

관련 문제