ArgumentNullException was unhandled. What is wrong?

Started by
9 comments, last by Joker123 11 years, 9 months ago
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";
}


protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
sprites = Content.Load<List<Sprite>>("Levelinf");

}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
foreach (Sprite sprite in sprites)
sprite.Draw(spriteBatch);
spriteBatch.End();
base.Draw(gameTime);
}
}
}


namespace SharedContent
{
public class Sprite
{
Vector2 position;
float rotation;
Vector2 scale;
string textureAsset;
Texture2D texture;
public Vector2 Position
{
get { return position; }
set { position = value; }
}
public float Rotation
{
get { return rotation; }
set { rotation = value; }
}
public Vector2 Scale
{
get { return scale; }
set { scale = value; }
}
public string TextureAsset
{
get { return textureAsset; }
set { textureAsset = value; }
}
[ContentSerializerIgnore]
public Texture2D Texture
{
get { return texture; }
}
public void Load(ContentManager content)
{
texture = content.Load<Texture2D>(textureAsset);
}
public void Draw(SpriteBatch batch)
{
batch.Draw(
texture,
position,
null,
Color.White,
rotation,
Vector2.Zero,
scale,
SpriteEffects.None,
0f);
}
}
}

And my XML file:

<?xml version="1.0" encoding="utf-8" ?>
<XnaContent>
<Asset Type="System.Collections.Generic.List[SharedContent.Sprite]">
<Item>
<Position>100 100</Position>
<Rotation>0</Rotation>
<Scale>.1 .1</Scale>
<TextureAsset>light</TextureAsset>
</Item>
<Item>
<Position>200 100</Position>
<Rotation>0</Rotation>
<Scale>.1 .1</Scale>
<TextureAsset>light</TextureAsset>
</Item>
<Item>
<Position>400 100</Position>
<Rotation>0</Rotation>
<Scale>.1 .1</Scale>
<TextureAsset>light</TextureAsset>
</Item>
</Asset>
</XnaContent>




WebRep

currentVote


noRating
noWeight
Advertisement
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.
The variable texture is always null, but I don't know what to change in the codesad.png I can upload my project if that would be easier for you.
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?
Always improve, never quit.
TextureAsset is set to "light", but texture is always null.
Here is the file.
http://depositfiles.com/files/kj4an4ef7

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.

This topic is closed to new replies.

Advertisement