I have a class implemented as this:
class Person
{
public int ID { get; set; }
public string FName { get; set; }
public string LName { get; set; }
public double[] Fees { get; set; }
public Person() { }
public Person(
int iD,
string fName,
string lName,
double[] fees)
{
ID = iD;
FName = fName;
LName = lName;
Fees = fees;
}
}
Then I am trying to test the code in a simple button click event, like this:
Person p = new Person();
p.ID = 1;
p.FName = "Bob";
p.LName = "Smith";
p.Fees[0] = 11;
p.Fees[1] = 12;
p.Fees[2] = 13;
for (int i = 0; i < p.Fees.Length; i++)
{
lstResult.Items.Add(p.ID + ", " + p.FName + ", " + p.LName + ", " + p.Fees[i]);
}
I'm keeping everything really basic and simple for the moment, just to get what I need working.
Visual studio gives this error when I run the program:
NullReferenceException was unhandled
The error has to do with the Fees array property of the Person object. I need to have the array as a property of the object so that I can associate fees with a particular person. So unless what I am trying to do here is impossible, I'd like to keep the same set up in the class.
- Am I not instantiating the object correctly?
- Do I need to do something more to initialize the array property?
- Can anyone see the issue I am having?
I'm willing to entertain ideas about using a dictionary or some other data structure. but ONLY if what I'm trying to do here is absolutely NOT possible.
I've looked around on Google and have had no luck. I've looked at old class notes and sample projects and no luck. This is my last hope. Someone please help. Thanks in advance to everyone.