简体   繁体   English

空引用异常C#get set

[英]Null Reference Exception C# get set

I think that I am misunderstanding the purpose of a get; 我认为我误解了获得的目的; set; 组; in C#. 在C#中。 I have a list of doubles that I am trying to populate and I am using the following code...Both of these are in the same class and when I try to run this, I get a Null reference exception when trying to populate the list. 我有一个我正在尝试填充的双打列表,我正在使用以下代码...这两个都在同一个类中,当我尝试运行它时,我在尝试填充列表时得到一个Null引用异常。 What exactly am I misunderstanding? 我到底误解了什么?

public List<double> NewData
{ get; set; }

public InfoFile(String fileName, String groupName)
{

    this.group = groupName;
    test = File.ReadAllLines(fileName);
    FileInfo label = new FileInfo(fileName);
    this.name = Path.GetFileName(fileName);
    isDrawn = false;

    for (int t = 2; t < test.Length; t++)
    {
        NewData.Add(double.Parse(test[t].Substring(6, 4)));
    }
}

You need to initialize/instantiate your list. 您需要初始化/实例化您的列表。

public List<double> NewData
{
    get;
    set;
}

public InfoFile(String fileName, String groupName)
{
    // initialize NewData to a new instance of List<double>
    NewData = new List<double>(); 

    this.group = groupName;
    test = File.ReadAllLines(fileName);
    FileInfo label = new FileInfo(fileName);
    this.name = Path.GetFileName(fileName);
    isDrawn = false;
    for (int t = 2; t < test.Length; t++)
    {
        NewData.Add(double.Parse(test[t].Substring(6, 4)));

    }

}

Docs: 文档:

In C# 3.0 and later, auto-implemented properties make property-declaration more concise when no additional logic is required in the property accessors. 在C#3.0及更高版本中,当属性访问器中不需要其他逻辑时,自动实现的属性使属性声明更简洁。 They also enable client code to create objects. 它们还使客户端代码能够创建对象。 When you declare a property as shown in the following example, the compiler creates a private, anonymous backing field that can only be accessed through the property's get and set accessors. 当您声明属性时,如以下示例所示,编译器将创建一个私有的匿名支持字段,该字段只能通过属性的get和set访问器进行访问。

http://msdn.microsoft.com/en-us/library/bb384054.aspx http://msdn.microsoft.com/en-us/library/bb384054.aspx


Depending on your scenario, as stated by @EricLippert in his comment to your OP, a setter is rarely used when exposing a List/Collection. 根据您的情况,正如@EricLippert在对您的OP的评论中所述,在公开List / Collection时很少使用setter。 It's more common to "Lazy Load" said List/Collection: “Lazy Load”更常见的是List / Collection:

public List<double> _newData;
public List<double> NewData
{
    get
    {
        if(_newData == null)
            _newData = new List<double>();

        return _newData;
    }
}

I believe you're just trying to do the following? 我相信你只是想做以下事情?

List<double> doubleList = new List<double>();
for (int t = 2; t < test.Length; t++)
{
    doubleList.Add(double.Parse(test[t].Substring(6, 4)));    
}

Edit: You may want to look at using a TryParse versus the Parse method depending on how confident you are in your input data. 编辑:您可能希望使用TryParse与Parse方法,具体取决于您对输入数据的信心。

Automatic properties initializes the property with the default value of that data type, and default value of all reference types are null. 自动属性使用该数据类型的默认值初始化该属性,并且所有引用类型的默认值均为null。 So basically the value of your NewData Property is null unless you assign something to it. 因此,除非您为其分配内容,否则NewData属性的值基本上为null。 For collections I would rather only make a get accessor as in your case too there is no need of a setter. 对于集合,我宁愿只在你的情况下制作一个get访问器,也不需要setter。 So my property would look like: 所以我的财产看起来像:

private List<double> newData = new List<double>();

public List<double> NewData { get{return newDate;} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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