Sign in to follow this  

[XNA] Can't define texture (This method does not accept null for this parameter.)

This topic is 3310 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

Hello, I'm using XNA as graphic library and trying to define (set?) a texture, but it says my texture is null, and I have no idea why it's null because I do define it (atleast what I see in my code). Error ArgumentNullException was unhandled. This method does not accept null for this parameter. Parameter name: texture This error points to the Draw method in the Player class Here is the method I'm using:
[SOURCE]
class Player
    {
        private string playerID = "test";
        private Texture2D playerTexture;
        private Color playerColor = Color.White;
        
        Convert convert = new Convert();

        public Texture2D PlayerTexture
        {
            get { return playerTexture; }
            set { playerTexture = value; }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(PlayerTexture, convert.ToVector2(PlayerX, PlayerY), PlayerColor);
        }
    }
[/SOURCE]
[/source] And I'm defining it in Game1.cs in LoadContent class by looping thru my player dictionary list like this:
[SOURCE]
static Dictionary<string, Player> pDictionary = new Dictionary<string, Player>();

protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            foreach (KeyValuePair<string, Player> kvp in pDictionary)
                kvp.Value.PlayerTexture = Content.Load<Texture2D>("player");
        }
[/SOURCE]
[/source] Then in Game1.cs in Draw class I loop thru my player dictionary with the Draw method which is in player object/class.
[SOURCE]
foreach (KeyValuePair<string, Player> kvp in pDictionary)
                kvp.Value.Draw(spriteBatch);
[/SOURCE]
[/source] As I can see its totally fine, but something must be really wrong here because the debugger says so. I really hope this is enought information for you guys to help me out here, and thanks for everything! :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Kylotan
I would assume the Content.Load<Texture2D> call is failing..?


Ohhh! I haven't thought about that, thank you so much. :D
I will be back if the problem is still there when I check this.

EDIT:
Okey, I can't find anything wrong, debugger doesnt say anything about it and it works perfectly if I just do like this:

Player myPlayer = new Player("test", 10, 10);

myPlayer.PlayerTexture = Content.Load<Texture2D>("player");
myPlayer.Draw(spriteBatch);



Could it be something with the get function I'm using for my dictionary?
I'll try to explain how the code looks like:
The dictionary is created and defined in Connections class (connections from clients), which holds some methods like Add, Edit and Remove that handles the dictionary. I also got a get function in the same class so the Game1 class can use it (for loading texture, drawing and updating) like this:

public Dictionary<string, Player> Dictionary
{
get { return cDictionary; }
}



then when I'm using the dictionary in Game1 class I use:
Connection.Dictionary

For example:

foreach (KeyValuePair<string, Player> kvp in connection.Dictionary)
kvp.Value.PlayerTexture = Content.Load<Texture2D>("player");



Could it be something that it doesn't updates correctly before it starts to draw?

[Edited by - Iderik on November 19, 2008 8:41:15 AM]

Share this post


Link to post
Share on other sites

This topic is 3310 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