-1

So I have a HealthPickup class and a Player class. Each class has this line of code:

public static Rectangle Hitbox = new Rectangle(Pos.X, Pos.Y, Tex.Width, Tex.Height);

My question is, why is there an error on the Player class and not the HealthPickup class? Edit: If i step over the Player hitbox, the HealthPickup causes the same error.Is it something to do with my Rectangle? The error is a TypeInitializationException which details follow as such:

System.TypeInitializationException was unhandled
  HResult=-2146233036
  Message=The type initializer for 'TestGame.Player' threw an exception.
  Source=TestGame
  TypeName=TestGame.Player
  StackTrace:
       at TestGame.RPG.LoadContent() in C:\Users\Pyroglyph\Documents\Visual Studio 2010\Projects\TestGame\TestGame\TestGame\RPG.cs:line 41
       at Microsoft.Xna.Framework.Game.Initialize()
       at TestGame.RPG.Initialize() in C:\Users\Pyroglyph\Documents\Visual Studio 2010\Projects\TestGame\TestGame\TestGame\RPG.cs:line 33
       at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun)
       at Microsoft.Xna.Framework.Game.Run()
       at TestGame.Program.Main(String[] args) in C:\Users\Pyroglyph\Documents\Visual Studio 2010\Projects\TestGame\TestGame\TestGame\Program.cs:line 15
  InnerException: System.NullReferenceException
       HResult=-2147467261
       Message=Object reference not set to an instance of an object.
       Source=TestGame
       StackTrace:
            at TestGame.Player..cctor() in C:\Users\Pyroglyph\Documents\Visual Studio 2010\Projects\TestGame\TestGame\TestGame\Player.cs:line 20
       InnerException: 

Edit: I stopped the error but I still need collision-detection, I'm using Console.Beep() to determine whether code runs or not. And, as per request, more code :)

public class HealthPickup : Microsoft.Xna.Framework.GameComponent
{
    public static Texture2D Tex;
    public static Point Pos = new Point(50, 50);
    public static Rectangle Hitbox = new Rectangle(Pos.X, Pos.Y, 32, 32);

    public HealthPickup(Game game) : base(game) { }

    public override void Initialize() { base.Initialize(); }

    public override void Update(GameTime gameTime)
    {
        //if (Tex.Bounds.Intersects(Player.Tex.Bounds)) //was testing other collision-detection methods
        //{
        //    Console.Beep(); //the only way I can check if it's being run or not
        //}
    }
}

In Draw():

GraphicsDevice.Clear(Color.White);

        spriteBatch.Begin();

        // Create Useless Rectangles
        Rectangle player = new Rectangle(Player.Pos.X, Player.Pos.Y, Player.Tex.Width, Player.Tex.Height);
        Rectangle healthPickup = new Rectangle(HealthPickup.Pos.X, HealthPickup.Pos.Y, HealthPickup.Tex.Width, HealthPickup.Tex.Height);

        // Draw Sprites
        spriteBatch.Draw(Player.Tex, player, Color.White);
        spriteBatch.Draw(HealthPickup.Tex, healthPickup, Color.White);

        spriteBatch.End();

        base.Draw(gameTime);

Player class:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace TestGame
{
    public class Player : Microsoft.Xna.Framework.GameComponent
    {
        public static Texture2D Tex;
        public static string Dir = "D";
        public static Point Pos = new Point(GraphicsDeviceManager.DefaultBackBufferWidth / 2, GraphicsDeviceManager.DefaultBackBufferHeight / 2);
        public static int speed = 4;
        public static Rectangle Hitbox = new Rectangle(Pos.X, Pos.Y, Tex.Width, Tex.Height);

        public Player(Game game): base(game) { }

        public override void Initialize() { base.Initialize(); }

        public override void Update(GameTime gameTime) { }
    }
}
Pyroglyph
  • 164
  • 5
  • 14

1 Answers1

1

This exception is thrown whenever a static constructor throws an exception, and in this case it seems like you have a NullReferenceException in the static constructor of the Player class.

EDIT:

The problem here is the Tex static field which isn't initialized before HitBox is created, and this explains the reason of the NullReferenceExcpetion that you get. So in order to solve the problem you have to initialize it, and you can do it in this way in the Player class:

public static Rectangle HitBox;
//other fields and methods

public override void LoadContent(){
    ContentManager content = MyGame.ContentManager;
    Tex = content.Load<Texture2D>("mytexture"); 
    //...

    base.LoadContent();      
    this.InitializeAfterLoadingContent();
}

private void InitializeAfterLoadingContent(){
    Hitbox = new Rectangle(Pos.X, Pos.Y, Tex.Width, Tex.Height);
}

As you can see I created another method cause a problem comes when the graphics resources are needed to initialize. In XNA the Initialize method is called before the LoadContent, so what you need to overcome this problem is to create a method and call it after loading content. Also your Game class can be done as:

public class MyGame : Microsoft.Xna.Framework.Game {
   public static ContentManager ContentManager;
   //...

   public MyGame( ) {
        //...

        ContentManager = Content;
    }

    protected override void Initialize(){
        // TODO: Add your initialization logic here

        base.Initialize();
    }

    protected override void LoadContent() {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        // TODO: use this.Content to load your game content here
        //...

        this.InitializeAfterLoadingContent();            
    }

    //you can call it however you want
    private void InitializeAfterLoadingContent(){
        //initialize your non-graphical resources by using the content info you need,
        //like the width and height of a texture you loaded
    }

    //...
}
Omar
  • 16,329
  • 10
  • 48
  • 66
  • So how would I be able to fix this NullReferenceException? Apparently my Rectangle/Hitbox is null, how would I fix that? – Pyroglyph Jun 22 '14 at 21:45
  • I need more code, if you can post it, I'd be glad to help you. – Omar Jun 22 '14 at 21:47
  • 1
    @Pyroglyph you can always step through the ctor or log it via the debugger. – bubbinator Jun 22 '14 at 21:51
  • @bubbinator I'm not sure what ctor is or how to log with the debugger? – Pyroglyph Jun 22 '14 at 21:56
  • @Fuex Thanks, and I put some more code in for you on the main question. – Pyroglyph Jun 22 '14 at 22:03
  • @Fuex Thanks for the updated answer, It doesn't crash now, but I'll ask another queston on how to detect collisions (the .Intersect doesn't work for me) on gamedev.stackexchange.com. – Pyroglyph Jun 23 '14 at 18:57
  • 1
    @Pyroglyph `ctor` is the abbreviation for the constructor. "To log to the debugger" just use `System.Diagnostics.Debug.Write` or `WriteLine` and put in the string of your choice. I usually aim for a variable name and it's value, but that doesn't work with reference types (classes). EDIT: the `System.Diagnostics.Debug.Write` will write the argument to the `Output` window in Visual Studio. – bubbinator Jun 24 '14 at 02:40