AbeeCrombie
AbeeCrombie

Reputation: 607

Accessing a List property of an object

I have a simple Class that I thought I could make more useful if I added a list property to store some values ( i dont want an array bc I dont know how long it will be)

public class MyClass
{
public double myProperty1;
public double myProperty2;
//etc
public  List<double> myList {get; set;}


}

but when I try to access this property, for example by

if x >y

newObject.myList.Add(x);

I get an error. Object reference not set to an instance of an object.

Any ideas what I am doing wrong here.

Upvotes: 2

Views: 1070

Answers (8)

Henk Holterman
Henk Holterman

Reputation: 273784

public class MyClass
{

    public double myProperty1;
    public double myProperty2;
    //etc
    public  List<double> myList {get; private set;}  // private set

    public MyClass()    // constructor
    {
           myList = new List<double>();    // create the list
    }
}

Your class instances need an actual List<> instance. The right place to create it is in a constructor. The private setter is usually a good idea but not essential.

Upvotes: 4

Adil
Adil

Reputation: 148180

Yoy need to initialize myList before you use could be in constructor.

public class MyClass
{

public MyClass()
{
    myList  = new List<double>();
}

public double myProperty1;
public double myProperty2;
//etc
public  List<double> myList {get; set;}   

}

Upvotes: 1

LightStriker
LightStriker

Reputation: 21024

You cannot use implicit properties with object like a List if you don't create an instance by yourself.

// Implicit declaration, a field is created in background, but the object isn't initialized.
public  List<double> myList {get; set;}

Instead:

// Explicit declaration, with me creating an instance
private List<double> myList = new List<double>();

public List<double> MyList
{
    get { return myList; }    
}

Another way would be to initialize your implicit declaration in your constructor. But I like to write my own field.

Upvotes: 1

Riccardo
Riccardo

Reputation: 1520

you must instantiate the List<double> object as a new object. Without the istantiation it's only a pointer.

Upvotes: 0

Artem Vyshniakov
Artem Vyshniakov

Reputation: 16465

You have to create an instance of the list. You can add initialization to the constructor of class:

public class MyClass
{
public double myProperty1;
public double myProperty2;
//etc
public  List<double> myList {get; set;}

public MyClass(){
   myList = new List<double>();
}

}

Upvotes: 1

Pranay Rana
Pranay Rana

Reputation: 176956

you need to initialize the list first to work properly, than add item like as below

if ( newObject.myList==null)
    newObject.myList= new List<double>();
newObject.myList.Add(x);

Upvotes: 1

Oded
Oded

Reputation: 499352

You need to initialize the list to something - it is not initialized, hence it is null.

In the class constructor, you could do:

public MyClass()
{
  myList = new List<double>();
}

Upvotes: 1

Roy Dictus
Roy Dictus

Reputation: 33149

You have to initialize the list. It is declared only, not initialized automatically.

Initialize it in the constructor:

public MyClass()
{
    MyList = new List<double>();
}

(BTW, to be compliant with naming conventions, properties must be Pascal-cased, not Camel-cased... So myList should be MyList.)

Upvotes: 1

Related Questions