0
\$\begingroup\$

I have a ArgumentNullException everytime I try to call the Draw() method of my Projectile class in the Game1.Draw() method.

In Game1.cs Initialize() method:

//PROJECTILES INITIALIZATION
        lesProjectiles = new List<Projectile>(20);
        for (int i = 0; i < lesProjectiles.Capacity; i++)
        {
            lesProjectiles.Add(new Projectile(projectileSprite,
                new Vector2(playerPosition.X - 15.5f, playerPosition.Y - 20)));
        }

This is the Projectile class constructor:

public Projectile(Texture2D texture, Vector2 startPos)
    {
            this.projectileSprite = texture;
            this.projectilePosition = startPos;
    }

The -15.5f and -20 values are for placing the projectile at the middle of my spaceship. (Not optimized logic ikr.)

Apart from my whole game logic, let's say I wanna draw the first element of my array. I'm doing that:

In Game1.cs Draw(GameTime gameTime) method:

        spriteBatch.Begin();
        lesProjectiles[0].Draw(gameTime, spriteBatch);
        spriteBatch.End();

And there is the Draw() method from my Projectile class :

public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(this.projectileSprite, this.projectilePosition,
            Color.White);
    }

The returned error: System.ArgumentNullException: Value cannot be null. Parameter name: texture

I don't understand why the projectileSprite values of my array elements are null, considering it's done in the initialization method whenever the constructor is called.

Thanks for further help, sorry if my explanations are bad

\$\endgroup\$
  • 1
    \$\begingroup\$ Is the sprite initialized in the Game1.cs Initialize() method? \$\endgroup\$ – Felsir Oct 28 '18 at 18:54
  • \$\begingroup\$ Oh well actually, the sprite is loaded in the LoadContent() method, which comes after the Initialize() method. Could it be the reason why it cannot find the texture ? \$\endgroup\$ – Muska Oct 29 '18 at 12:45
  • \$\begingroup\$ I think it is; try moving the init loop after you load the texture and see if that helps. From my own experience, I use the load content to initialize any game content. I only use the initialize() method for initializing some values or manager classes. \$\endgroup\$ – Felsir Oct 29 '18 at 14:01
  • \$\begingroup\$ I tried to put my Initialize() method after the LoadContent() one, but the exception still remains. I'll keep the topic updated when I find a solution. \$\endgroup\$ – Muska Oct 29 '18 at 14:38
  • \$\begingroup\$ I created a "SetTexture(Texture2D texture)" method in my Projectile class so it just assigns the texture given in parameter to the sprite field of my class. If I call it in my Draw() method and then try to draw my rocket, it works all fine. Hm... \$\endgroup\$ – Muska Oct 29 '18 at 14:41
0
\$\begingroup\$

Ok I found a fix after multiple tries.

It seems like, even if I use the Content.Load in my LoadContent() method and loads a sprite into variable, I can't use it in both Update() and Draw() method. The texture isn't found.

My fix is not perfect (I guess), but it works. Instead of actually loading my sprite.png in the LoadContent() method, I did it in my Initialize() method, just before my For structure. Here it is:

In Game1.cs Initialize() method:

        //PROJECTILES INITIALIZATION
        projectileSprite = Content.Load<Texture2D>("rocket");

        lesProjectiles = new List<Projectile>(20);
        for (int i = 0; i < lesProjectiles.Capacity; i++)
        {
            lesProjectiles.Add(new Projectile(projectileSprite,
                new Vector2(playerPosition.X - 15.5f, playerPosition.Y - 20)));
        }

Now I can Draw the sprites from my array without any issue. Thanks to Felsir, he got me on the way.

\$\endgroup\$

Your Answer

By clicking "Post Your Answer", you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.