[Solved] Can't figure out this NullReferenceException on a Texture2D

Hi,

First of all thank you for stopping by and taking a look at my issue. I’m new to MonoGame and I have a hard time with this NullReferenceException, I have 3 files currently, Core, World and Sprite. My issue come from the third one at line 30 where I initialize my Texture2D. Here is my code:

Core.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace rpg
{
    public class Core : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;

        World world;

        public Core()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            IsMouseVisible = true;
        }

        protected override void Initialize()
        {
            // TODO: Add your initialization logic here
            world = new World();

            base.Initialize();
        }

        protected override void LoadContent()
        {
            Globals.content = this.Content;
            Globals.spriteBatch = new SpriteBatch(GraphicsDevice);

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

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
                Exit();

            // TODO: Add your update logic here
            world.Update();

            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            // TODO: Add your drawing code here
            Globals.spriteBatch.Begin();

            world.Draw();

            Globals.spriteBatch.End();

            base.Draw(gameTime);
        }
    }
}

World.cs

using System;
using System.Collections.Generic;
using System.Text;

namespace rpg
{
    class World
    {
        Sprite sprite;

        public World()
        {
            sprite = new Sprite("Graphics/Sprites/player", 0, 0, 16, 25, 1, 0f);
        }

        public virtual void Update()
        {
            
        }

        public virtual void Draw()
        {
            sprite.Draw();
        }
    }
}

Sprite.cs

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace rpg
{
    /// <summary>
    /// Draw a sprite on the world
    /// </summary>
    class Sprite
    {
        string path; 
        Texture2D texture;

        int posX, posY, width, height;
        float scale;
        Rectangle position;

        Vector2 origin;

        Color color;
        SpriteEffects spriteEffects;

        float layer;

        
        public Sprite(string cPath, int cPosX, int cPosY, int cWidth, int cHeight, float cScale, float cLayer)
        {
            // Initialize the sprite texture
            path = cPath;
            texture = Globals.content.Load<Texture2D>(path); // <= NullReferenceException

            // Initialize the sprite position and scale
            posX = cPosX;
            posY = cPosY;
            width = cWidth * (int)cScale;
            height = cHeight * (int)cScale;
            position = new Rectangle(posX, posY, width, height);

            // Set the origin to the middle of the sprite
            origin = new Vector2(texture.Bounds.Width / 2, texture.Bounds.Height / 2);


            color = Color.White;

            spriteEffects = new SpriteEffects();

            layer = cLayer;
        }

        public virtual void Draw()
        {
            if(texture != null)
            {
                Globals.spriteBatch.Draw(texture, position, null, color, 0f, origin, spriteEffects, layer);
            } 
        }
    }
}

I have my image player in “/Graphics/Sprites/” and when I initialize and draw my sprite in Core.cs it works just fine.

Thank you !

In MonoGame, Initialize is called before LoadContent. By the time you call new World, loadContent hasn’t been called, so Globals.content is null.

Hello KakCAT,

Thank you for your help, I created a new world in loadContent and it works.
At least I know the difference between Initialize and LoadContent now.

Have a good day!