简体   繁体   中英

NullReferenceException on array property of object

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.

You are missing the array initialization as others have pointed out.

p.Fees = new double[3];

But, a generic List would be better suited for almost all places where you'd use an array. It is still the same data structure.

A List would automatically shrink and expand as you add and remove items to it, removing the need to manage the size of the array yourself.

Consider this class (note that you need to import System.Collections.Generic)

    using System.Collections.Generic;

    class Person
    {
    public int ID { get; set; }
    public string FName { get; set; }
    public string LName { get; set; }
    public List<double> Fees { get; set; }

    public Person() 
    { }

    public Person(
        int iD,
        string fName,
        string lName,
        List<double> fees)
    {
        ID = iD;
        FName = fName;
        LName = lName;
        Fees = fees;
    }
}

Now here's how your test method should look

        Person p = new Person();
        p.ID = 1;
        p.FName = "Bob";
        p.LName = "Smith";
        p.Fees = new List<double>();
        p.Fees.Add(11);
        p.Fees.Add(12);
        p.Fees.Add(13);

        for (int i = 0; i < p.Fees.Count; i++)
        {
            lstResult.Items.Add(p.ID + ", " + p.FName + ", " + p.LName + ", " + p.Fees[i]);
        }

You will still need to create a new instance of the Fees property, but you don't have to worry about initializing the size of the array now. For bonus points you can easily transform it into an array if you need to by using ToArray()

p.Fees.ToArray();

In your default constructor, which is the one you're invoking, you do not initialize fees .

public Person() {
  this.Fees = new double[10]; // whatever size you want
}

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;

should be translated to this

Person p = new Person(1,"Bob","Smith",new double[]{ 11, 12, 13 });

Add following line

p.Fees = new double[3];

before

p.Fees[0] = 11;

You need to initalise Fees. eg

Person p = new Person();
p.ID = 1;
p.FName = "Bob";
p.LName = "Smith";
p.Fees = new double[] {11, 12, 13};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM