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.

0

1 Answer 1

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...

Sign up to request clarification or add additional context in comments.

5 Comments

+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.
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
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 ;)
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 :(
you should access the GraphicsDevice object from your game object

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.