views:

540

answers:

4
private List<string> _Baseline = new List<string>();

public List<string> Baseline 
{
    get { return _Baseline; }
    set { _Baseline = value; }
}

How can I set this property? It does not let me add using the add method; it throws an "object reference null" error.

+1  A: 

Do you initialize the class (using new) that holds this property before use?

There are two possible cases (assuming your code is in MyClass class):

//External code:
MyClass x = new MyClass();
x.Baseline = null; // Somewhere it'll be set to null.
x.Baseline.Add("Something"); // NullReferenceException

Or:

//External code:
MyClass x = null; // Somewhere the class itself is set to null.
x.Baseline.Add("Something"); // NullReferenceException
Mehrdad Afshari
he wouldn't otherwise be able to call it (compile time ...), and the sample already shows it is initialized in the backing variable
eglasius
This certainly compiles: MyClass x = null; x.Baseline.Add("null reference exception");
Mehrdad Afshari
+4  A: 

It should work if you did what you wrote here. I guess you are using generics, and I can't see them in your post.

If you have a complex expression, split it. For example, change ObjectA.Prop.Other.Xyz.Add(..) to:

SomeClass a = ObjectA.Prop;
SomeClass2 b = a.Other;
SomeClass3 c = b.Xyz;
c.Add(...)

this way you will find quickly where the null reference is.

Diego Jancic
A: 

You might need to use IList:

private IList<string> _Baseline = new List<string>();

public IList<string> Baseline 
{
    get { return _Baseline; }
    set { _Baseline = value; }
}
Nick
Nope, this should *not* be the problem.
Mehrdad Afshari
There's no difference, it's the same either way. Both ways works.
Diego Jancic
what is it with the use of * to emphasize things these days, i don't like it one bit.
Nick
I'm not too sure if THIS is any better than *this* or _this._ It's a way to show emPHASis in plain text. Unfortunately, the English language doesn't give us what we need to do it otherwise.
Michael Meadows
Thanks for the history lesson, but I could still do without whatever emphasis symbol is popular at the time.
Nick
*this* is probably because SO uses single asterisk for italics in its markdown. The 10K+ club members like Mehrdad must be pretty good at writing markdown. :)
Michael Meadows
@Michael Meadows: Nice answer.
configurator
Oh, but *this* has been is use far before markdown.
configurator
A: 

There are 3 possibilities:

  1. your list<string> is null
  2. the object containing your list<string> is null
  3. the item you are inserting is null

1 is addressed when you assign a new List to it

2 and 3 we can not ascertain from the code you post here.

if you do not intend to allow assignment of a new list object outside of your class, then you do not, as noted elsewhere, need a setter. You can either remove it or declare it private or protected, like this....

public List<string> Baseline 
{
    get { return _Baseline; }
    protected set { _Baseline = value; }
}
Muad'Dib
3 wouldn't cause an exception.
configurator
@Chris: configurator is right. You *can* insert nulls in a list.
Mehrdad Afshari
yeh, i know, but his exception could be something caused by trying to call the null object that he inserted into the collection, hence the possability. just laying out his options.
Muad'Dib
Thanks everyone, my mistake, my code is working fine and is same as it was. I got confused while dealing with more than 80 properties.
Sunilk