er

er

  • NA
  • 5
  • 0

List gets null while it should not be?

Jan 29 2010 7:15 AM
I have the following class:

 public class MyClass
{
private List<Queue> listOfItems;
private int someInteger;

public MyClass()
{
List<Queue> listOfItems = new List<Queue>();
listOfItems.Add(new Queue()); // THIS WORKS
someInteger = 0; // THIS WORKS
}

public void SetValue()
{
someInteger = 1; // THIS WORKS
listOfItems.Add(new Queue()); // THIS FAILS, WHY?!?
}

}

I use the following:
MyClass test = new MyClass();
test.SetValue();


This generates the following error at the underlined line of the class code: System.NullReferenceException: Object reference not set to an instance of an object.

Why is that and how to fix it? Thanks for any reply!

Answers (3)