2

I think that I am misunderstanding the purpose of a get; set; in 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. 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)));
    }
}
1
  • 3
    Note that it is rare to make a property of collection type with a public setter. Do you intend the user of your class to be able to replace the entire list with a different list? Do you intend them to be able to modify the list without changing the identity of the list? Or none of the above? Commented Jun 10, 2013 at 22:25

3 Answers 3

12

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. 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.

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. It's more common to "Lazy Load" said List/Collection:

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

        return _newData;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

it means you should set: NewData = new List<double>(); before Add method.
Thank you! It's been a long day with too little coffee!
With more recent version of C# all you have to do is go public virtual List<double> NewData { get; set; } = new List<double>(); This sets up a new list regardless of whether or not any data is available to populate it, allowing you to avoid a null reference, as well as the need to do a NewData = new List<double>(); before actually populating it with data.
0

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.

Comments

0

Automatic properties initializes the property with the default value of that data type, and default value of all reference types are null. So basically the value of your NewData Property is null unless you assign something to it. For collections I would rather only make a get accessor as in your case too there is no need of a setter. So my property would look like:

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.