Sign in to follow this  

ArgumentNullException was unhandled. What is wrong?

This topic is 1983 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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):
[CODE]
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);
}
}
}[/CODE]
[CODE]
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);
}
}
}[/CODE]
And my XML file:
[CODE]
<?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>[/CODE]



WebRep

currentVote


noRating
noWeight

Share this post


Link to post
Share on other sites
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 [i]should [/i]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.

Share this post


Link to post
Share on other sites
The variable texture is always null, but I don't know what to change in the code[img]http://public.gamedev.net//public/style_emoticons/default/sad.png[/img] I can upload my project if that would be easier for you.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
[quote name='arkane7' timestamp='1341591410' post='4956363']
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?
[/quote]

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?

Share this post


Link to post
Share on other sites
[quote name='Joker123' timestamp='1341603045' post='4956435']
TextureAsset is set to "light", but texture is always null.
[/quote]

So this
[code]
texture = content.Load<Texture2D>(textureAsset);

[/code]

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?

Share this post


Link to post
Share on other sites
I get the error message in this line :
[CODE]batch.Draw(
texture,
position,
null,
Color.White,
rotation,
Vector2.Zero,
scale,
SpriteEffects.None,
0f);[/CODE]
I changed the line [CODE]texture = content.Load<Texture2D>(textureAsset);[/CODE] to [CODE]texture = content.Load<Texture2D>("light");[/CODE] but it changed nothing. Always the same error message.
[CODE]
+ 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
[/CODE]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

This topic is 1983 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this