I am having problems trying to build and debug my program. Here is what I have that causes me errors:
class NpcData
{
public int X; // actual X coordinate
public int Y; // actual Y coordinate
public byte pathMovement;
public byte pathMarker;
public float frameDelay;
public string[] speech = new string[4];
public string name;
public Texture2D visual;
}
And in my main() function, I declare it as this:
NpcData[] player = new NpcData[2];
However, in my other classes and methods, the first instance my application executes a command like:
player[0].name = "my Name";
Results in an error stating: Object reference not set to an instance of an object.
Tips: use the "new" keyword to create an object instance.
Is there something I am missing? I am using XNA 2.0 if that helps any.
Thanks
Help with object instantiation and array classes
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.

AlanRank #669 · 3.6K reputationPosted Oct 5, 2008, 9:46 AM EDT
Currently, the elements of your NpcData array are both null which is the default for all reference types (i.e. classes). To remedy the situation, change this line:
NpcData[] player = new NpcData[2];
to this:
NpcData[] player = new NpcData[2]{new NpcData(), new NpcData()};
James Kurth5 reputationPosted Oct 5, 2008, 3:25 PM EDT
Thank you,
JimHouston