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
Loading
AlanPosted Oct 5, 2008, 1:46 PM
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 KurthPosted Oct 5, 2008, 7:25 PM
Thank you,
JimHouston