1

Now the problem isnt that i dont know what the error means but i cannot for the life of god figure out what could possibly be the cause of this. I've searched alot looking for answers, but the solution to my anguish eludes me. ive many times tried to overcome my obstacle but failed. Now i look upon you hoping for enlightenment, may you help my brain rest in peace :)

Here comes the problem: Game1 runs and does its thing with my class named 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
    }
}

Ive removed code that was unimportant so it would be easier to view. th problem is: Object reference not set to an instance of an object.

Thanks for any help and advice.

user1344948
  • 141
  • 1
  • 3
  • 11

1 Answers1

3

I think that you don't initialize the "content" variable.

if you instance a MenuComponent with the default no parameters constructor, content is not assigned.. so it will be equal to null...

Blau
  • 5,742
  • 1
  • 18
  • 27
  • +1, I think this is the only plausible way to get the error message described. @user1244948: I'd learn how to use your debugger a little more. When it crashes here, you can hold your mouse over variables to see their current state. Simply holding your mouse over `content` would have indicated the problem on the spot. – John McDonald Apr 19 '12 at 20:59
  • Thanks, yes that was the problem. I used it im Game1 class before it got a value :) Though i hit the same problme with another peice of code looking like this: pPosition = new Vector2((graphics.PreferredBackBufferWidth - pTexture.Width) / 2, (graphics.PreferredBackBufferHeight - pTexture.Height) / 2); In my LoadContent method, trying to solve it but but... Thanks for helping me get one step futher along the way :D – user1344948 Apr 19 '12 at 21:07
  • you should follow @john mcdonald advice, use the debugger... and don't use graphics outside the game constructor... use GraphicsDevice.Viewport.Width/Height instead... and you should sure that pTexture is not null ;) – Blau Apr 20 '12 at 00:30
  • problem with GraphicsDevice.Viewport.Width/Height is that it always gives me an error: An object reference is required for the non-static field, method, or property :( – user1344948 Apr 20 '12 at 10:39
  • you should access the GraphicsDevice object from your game object – Blau Apr 20 '12 at 12:31