I have written a class to load an XML file but I always get this error message:
ArgumentNullException was unhandled
This method does not accept null for this parameter.
Parameter name: texture
What is wrong in the Sprite class?
Here is the entire code of Game1 and Sprite class(where I load the XML file):
namespace WindowsGame16
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
Texture2D licht;
List<Sprite> sprites = new List<Sprite>();
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}
Not trying to seem unhelpful, but you are in a much better position to debug this than anybody here.
Build with debug configuration, and you should get a very helpful stack trace from the exception. This should point you directly to where "texture" is null and trying to be used. Set a breakpoint to see where this texture should be getting set but is being set to null (or not being set at all), and you should have located your bug.
If you are still having trouble, at least you will have narrowed down where the error is and why it is occuring, an can try to get more specific help here.
I'm away from by develpoment workstation at the moment, but maybse somebody else here would be willing to help if you upload?
In the meantime, if you set a breakpoint in your Sprite.Load method (where it tries to load the texture), is the texture getting loaded there properly? If not, perhaps it cannot find the file due to incorrect path, or perhaps "TextureAsset" is not being set correctly?
perhaps it is because you do not have a constructor for the sprite class. Maybe you didn't set the texture to a non-null texture2d object?
The sprite class has no constructor explicitly defined, but the list of sprites is apparently built by the Content.Load<List<Sprite>>() method. Set a breakpoint in the debugger right after sprites is initialized and see if any of the values are being loaded correctly in the first place. Also stack traces are your friend, where is texture trying to be dereferenced as a null object: the draw call, or somewhere else?
Hazard Pay :: FPS/RTS in SharpDX (gathering dust, retained for... historical purposes) DeviantArt :: Because right-brain needs love too (also pretty neglected these days)
TextureAsset is set to "light", but texture is always null.
So this
texture = content.Load<Texture2D>(textureAsset);
is the culprit? You put a breakpoint here, and verified the textureAsset name as correct, but after stepping through "texture" is still null?
I don't remember offhand if content.Load throws an exception if a resource cannot be located (I kind of thought it did), but possibly it's not finding the file due to incorrect path?
I get the error message in this line : batch.Draw(
texture,
position,
null,
Color.White,
rotation,
Vector2.Zero,
scale,
SpriteEffects.None,
0f);
I changed the line texture = content.Load<Texture2D>(textureAsset); to texture = content.Load<Texture2D>("light"); but it changed nothing. Always the same error message.
+ Color.White {R:255 G:255 B:255 A:255} Microsoft.Xna.Framework.Color
+ Vector2.Zero {X:0 Y:0} Microsoft.Xna.Framework.Vector2
+ batch {Microsoft.Xna.Framework.Graphics.SpriteBatch} Microsoft.Xna.Framework.Graphics.SpriteBatch
+ position {X:100 Y:100} Microsoft.Xna.Framework.Vector2
rotation 0.0 float
+ scale {X:0,1 Y:0,1} Microsoft.Xna.Framework.Vector2
texture null Microsoft.Xna.Framework.Graphics.Texture2D
+ this {SharedContent.Sprite} SharedContent.Sprite
Yes, but did you specifically set a breakpoint in the Sprite.Load method to make sure it was getting executed? From the source you posted, it seems that one of these things must be happening:
Sprite.Load isn't being called (for at least one of the sprites, if it is supposed to load 3 sprites, make sure it is being called 3 times)
Content.Load is returning null
"texture" field is being nulled out somehwere/somehow after Sprite.Load
You can also try simplifing things - try creating only one sprite, or manually creating a sprite (without using XML), etc. to see where the flaw is.