0

I'm using XNA and attempting to load an image using a string

image = Game.Content.Load<Texture2D>(playerCharacter.image);

the character class is abstract, the PlayerCharacter classes are all derived from it and the image variable is set to something like "PlayerSprites/Char1"

I get a null exception when running this code. The path is correct, but I don't know if a path is the correct way to do this.

Jake
  • 337
  • 1
  • 3
  • 15
  • 2
    Possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Sayse Jan 25 '16 at 14:47
  • Hi, try to load the texture in your main game, if it works then check your game property in the context above if it is null. If not check that you added the asset to your content project with the same path (e.g. 'PlayerSprites'-Folder -> 'Char1'-Image) – Bashn Jan 25 '16 at 15:50
  • I solved it. The asset loads fine without using the playerCharacter.image variable. There was a typo messing with the character class – Jake Jan 26 '16 at 01:45

1 Answers1

0

Don't use the file path. Use the asset name. Click on the asset in the solution explorer. You'll see a bunch of properties pop up. Look at the one that says "Asset Name". That is literally the string you pass in to the loadcontent method. Hope I helped!

Daniel G
  • 245
  • 4
  • 15