I need the texture2D's bounds but it is null when I need the data

I need to use the texture’s size to center it in the Y axis, but when I need that data, the texture is still null.

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

public class Player : Drawable {
    private float _speed = 600f;

    private void SetPosition() { 
        this.position = new Vector2(
            (Engine.Instance.Window.ClientBounds.Height - this.texture.Height) / 2

    private void Move(GameTime gameTime) { 
        float deltaTime = (float) gameTime.ElapsedGameTime.TotalSeconds;

        if (Keyboard.GetState().IsKeyDown(Keys.W)) {
            this.position.Y -= _speed * deltaTime;
        if(Keyboard.GetState().IsKeyDown(Keys.S)) {
            this.position.Y += _speed * deltaTime;

    public override void Load() {
        this.texture = Engine.Instance.Content.Load<Texture2D>("Sprites/Player"); 


    public override void Update(GameTime gameTime) {

    public override void Draw(GameTime gameTime) {
        Engine.Instance._batch.Draw(this.texture, this.position, Color.White);

As you can see, I put the SetPosition method inside Load (which’ll be inside LoadContent()), but I think it is not the best practice. I also thought about using a fixed number (64) but that’s not good either, so what can I do?

Hey @LasZus welcome to the community.

Looking at the code you provided I can’t see any reason why the this.texture would be null in the SetPosition() method, especially ifSetPosition() is only called from Load()

WIthout knowing more about the code I can only make educated guesses. LIke it seems that the texture field is part of the Drawable class that is being inherited? But I don’t know what, if anything is happening when that texture is set (or if it’s even a field, or a property where the setter could do additional logic)

At this point, the best advice I could give would be to set breakpoints in the code and use the debugger to step through and follow the logic to ensure that all values are as you expect them to be and that nothing is affected the texture before SetPosition() is called.

Could you also provide the exact error message given?

Hello, I’m calling the SetPosition() method inside Load, so I can use the texture’s size, but I don’t think that’s a good practice. That was my question, is it bad? And if it is, then how can I fix that?

No it’s not bad practice per se.

What you’ve done is basically used a method called Load as the initialization of the properties of the Playerclass. So it’s doing all the things that need to be done to initialize it. You pulled the code for setting the initial position out into it’s own function, which could be argued as good practice since it keeps to the single purpose principle, but also could be argued as bad practice since it literally just initializes the position, why put it in a second method that you have to search around for when refactoring or debugging.

In the end, a choice like this is just comes down to how you want to structure the code stylistically. If SetPosition is only ever called from Load and never anywhere else, maybe it doesn’t need to be it’s own function, but if you want to keep to the single purpose principle, then maybe it does.

Maybe create a method called Get_Center that takes any texture as an overload, and returns a vector2 from that overload…

But if your code is getting a null from a texture, your code is probably running out of order. ie you set the position before you load the image.

Heres another possibility if you feel sure about your code… I’ve had issues with this. The code editor can be showing you a version APART from the one you are actually running… But you can test this by changing a draw color, and see if your edits come through,