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\$
5
  • 1
    \$\begingroup\$ Is the sprite initialized in the Game1.cs Initialize() method? \$\endgroup\$
    – Felsir
    Commented Oct 28, 2018 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
    Commented Oct 29, 2018 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
    Commented Oct 29, 2018 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
    Commented Oct 29, 2018 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
    Commented Oct 29, 2018 at 14:41

2 Answers 2

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\$
0
\$\begingroup\$

Be sure you have something like this in your code:

Game(1)
{
Content.RootDirectory = "Content";
sContent = Content; 
}

In the book "Learn 2D Game Development with C#" on page 43 they left that out.

\$\endgroup\$
1
  • \$\begingroup\$ its off-topic to give source of learning. but if its really needed, you can give link of book on amazon or... \$\endgroup\$
    – virtouso
    Commented Oct 4, 2019 at 8:29

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .