【问题标题】:NullReferenceException on array property of object对象的数组属性上的 NullReferenceException
【发布时间】:2013-10-08 07:21:47
【问题描述】:

我有一个这样实现的类:

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;
    }
}

然后我尝试在一个简单的按钮单击事件中测试代码,如下所示:

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]);
}

我暂时保持一切非常基本和简单,只是为了得到我需要的工作。

Visual Studio 在我运行程序时出现此错误:

NullReferenceException was unhandled

错误与 Person 对象的 Fees 数组属性有关。我需要将数组作为对象的属性,以便我可以将费用与特定的人相关联。因此,除非我在这里尝试做的事情是不可能的,否则我想在课堂上保持相同的设置。

  • 我没有正确实例化对象吗?
  • 我需要做更多的事情来初始化数组属性吗?
  • 谁能看到我遇到的问题?

我愿意接受有关使用字典或其他数据结构的想法。但只有当我在这里尝试做的事情绝对不可能时。

我在 Google 上环顾四周,但运气不佳。我看过旧的课堂笔记和示例项目,但没有运气。这是我最后的希望。有人请帮忙。在此先感谢大家。

【问题讨论】:

  • 需要分配数组:p.Fees = new double[3];

标签: c# arrays object properties


【解决方案1】:

正如其他人指出的那样,您缺少数组初始化。

p.Fees = new double[3];

但是,通用 List 会更适合几乎所有使用数组的地方。 还是一样的数据结构。

当您向其中添加和删除项目时,列表会自动收缩和扩展,从而无需自己管理数组的大小。

考虑这个类(注意需要导入 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;
    }
}

现在你的测试方法应该是这样的

        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]);
        }

您仍然需要创建 Fees 属性的新实例,但您现在不必担心初始化数组的大小。对于奖励积分,如果需要,您可以使用 ToArray() 轻松将其转换为数组

p.Fees.ToArray();

【讨论】:

  • 谢谢!很好的答案!
【解决方案2】:

在您调用的默认构造函数中,您不初始化fees

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

【讨论】:

    【解决方案3】:

    这个

    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;
    

    应该翻译成这个

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

    【讨论】:

      【解决方案4】:

      添加以下行

      p.Fees = new double[3];
      

      之前

      p.Fees[0] = 11;
      

      【讨论】:

        【解决方案5】:

        您需要初始化费用。例如

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

        【讨论】:

          猜你喜欢
          • 2013-10-04
          • 2022-12-17
          • 1970-01-01
          • 2010-09-22
          • 1970-01-01
          • 2021-06-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多