2

I've been trying to fix this class null problem for little bit. I don't understand it. It say that my class is null for some reason. Which then make my LoadContent null too. The additional information says: Object reference not set to an instance of an object.

CharacterInfo Chara;//Chara Class is Null
Chara.LoadContent(Content);// Error pointing to 

//This is behind Chara.LoadContent(Content);
texture = Content.Load<Texture2D>("Art/BlueAnvil");
HealthBar = Content.Load<Texture2D>("Art/HealthBar");

Started happening after I add the LoadContent constructor, to the class, and started to using Chara.LoadContent(Content);

Please help me expand my knowledge in C# so I can remember how to fix this.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
Howllo
  • 33
  • 7
  • @John Saunders You should learn what duplicate means. This was was not a duplicate of your post. For one, I didn't copy the work as in yours. So it can't be duplicate. Second I ask about the class null, and didn't ask what a null is. And I now know what a NullReferenceException is after fixing a lot of them. Your post was useless for me(Learn about nulls yesterday). Just because you create a post, doesn't mean that it will help other people. When I was talking about the Null; I talking about XNA. So next time understand what the person is asking before you start marking stuff up. – Howllo Aug 20 '14 at 16:37
  • This is a duplicate, all right. Note where it says "**This question already has an answer [here](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it)**". The answer you accepted is in the first answer of the linked duplicate: create an instance of the class. – John Saunders Aug 20 '14 at 18:57
  • BTW, you're confused on the difference between a "class" and an "object" or "instance of the class". In your case, the class is not null (a concept that makes no sense), rather your reference does not reference an instance of the class. Not until you initialize it with `new CharacterInfo()`. – John Saunders Aug 20 '14 at 18:59

1 Answers1

0

You need to instantiate your object:

CharacterInfo Chara = new CharacterInfo();

At this point, you're only declaring the variable, but you're not assigning an actual value to it, hence the exception.

If LoadContent is a static factory method that creates CharacterInfo objects based on the value of the passed parameter, it should be:

CharacterInfo Chara = CharacterInfo.loadContent(Content);
Robby Cornelissen
  • 91,784
  • 22
  • 134
  • 156