对象的数组属性上的NullReferenceException [英] NullReferenceException on array property of object

查看:68
本文介绍了对象的数组属性上的NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个这样实现的类:

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会出现此错误:

Visual studio gives this error when I run the program:

NullReferenceException未处理

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

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.

  • 我没有正确实例化该对象吗?
  • 我需要做更多的事情来初始化array属性吗?
  • 有人可以看到我遇到的问题吗?

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

我在Google上四处浏览,但没有运气.我看过旧课笔记和示例项目,没有运气.这是我最后的希望.有人请帮忙.在此先感谢大家.

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.

考虑此类(请注意,您需要导入System.Collections.Generic)

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

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

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();

这篇关于对象的数组属性上的NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆