-1

I am trying to draw to screen my player and some entitys but I'm getting null exception on my player's update method...

my SpriteManager

public override void Update(Microsoft.Xna.Framework.GameTime gameTime)
    {
        player.Update(gameTime, Game.Window.ClientBounds);

        foreach(Sprite s in spriteList)
        {
            s.Update(gameTime, Game.Window.ClientBounds);
            if(s.collisionRect.Intersects(player.collisionRect))
                Game.Exit();
        }
        base.Update(gameTime);
    }

and my player class this is how I handle my inputs

    public override Vector2 direction {
        get {
            Vector2 inputDirection = Vector2.Zero;

            KeyboardState keyboardState = Keyboard.GetState();
            if (keyboardState.IsKeyDown(Keys.Left))
                inputDirection.X -= 1;
            if (keyboardState.IsKeyDown(Keys.Right))
                inputDirection.X += 1;
            if (keyboardState.IsKeyDown(Keys.Up))
                inputDirection.Y -= 1;
            if (keyboardState.IsKeyDown(Keys.Down))
                inputDirection.Y += 1;

            return inputDirection * speed;
        }
    }

and here is the update which causes the null exception

        public override void Update(GameTime gameTime, Rectangle clientBounds) {
        position += direction;
        if(position.Y < 0)
            position.Y = 0;
        if(position.X < 0)
            position.X = 0;
        if(position.Y > clientBounds.Height - frameSize.Y)
            position.Y = clientBounds.Height - frameSize.Y;
         if(position.X > clientBounds.Width - frameSize.X)
            position.X = clientBounds.Width - frameSize.X;
        base.Update(gameTime, clientBounds);
    }
  • What specific line is causing your NullPointer? I also don't see in your code where the frameSize variable is initialized. – britter Mar 29 '18 at 14:05
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Sebastian Hofmann May 01 '18 at 06:14

1 Answers1

0

First thing I notice is that this needs to be tabbed:

    public override void Update(GameTime gameTime, Rectangle clientBounds) {
        position += direction;
        if(position.Y < 0)
            position.Y = 0;
        if(position.X < 0)
            position.X = 0;
        if(position.Y > clientBounds.Height - frameSize.Y)
            position.Y = clientBounds.Height - frameSize.Y;
         if(position.X > clientBounds.Width - frameSize.X)
            position.X = clientBounds.Width - frameSize.X;
        base.Update(gameTime, clientBounds);
    }

Second thing I notice is that your player class returns: inputDirection * speed;
but there's no variable set to speed to accept that return.

Steven
  • 1,996
  • 3
  • 22
  • 33