Object null reference error when adding list of objects

//I have two classes FooOptions.......................

   public class FooOptions
    {
        public int ID { get; set; }        
        public int Area { get; set; }
    }


And FooList that has as a property a list of FooOptions


    public class FooList
    {
        public int ID { get; set; }
        public List< FooOptions > ListOfFoos { get; set; }
    }

//I  have a nested foreach loop

Foreach (……..)
{
    FooList thisList = new FooList();
    thisList.ID = 1;
           Foreach (……..)
             {
             FooOptions thisOption = new FooOptions ();
               thisOptions.ID = 2;
               thisOptions.Size = 3;  
               thisList. ListOfFoos.Add(thisOption);
             }
}


However if I run the code I get a error "object reference not set to an instance of an object."
when it hits

          "thisList. ListOfFoos.Add(thisOption);"

Code snippet below
//I have two classes FooOptions

   public class FooOptions
    {
        public int ID { get; set; }        
        public int Area { get; set; }
    }

And FooList that has as a property a list of FooOptions
    public class FooList
    {
        public int ID { get; set; }
        public List< FooOptions > ListOfFoos { get; set; }
    }

//I  have a nested foreach loop

Foreach (……..)
{
    FooList thisList = new FooList();
    thisList.ID = 1;
           Foreach (……..)
             {
	       FooOptions thisOption = new FooOptions ();
               thisOptions.ID = 2;
               thisOptions.Size = 3;  
               thisList. ListOfFoos.Add(thisOption);
             }
}

Open in new window

ToString1Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

xav056Commented:
its because ListOfFoos  is not being instantiated
AndyAinscowFreelance programmer / ConsultantCommented:
I don't see how that code will compile - thisOptions isn't defined anywhere.   (Please post your real code - you might accidentally fix the error in using pseudo code))
Erick37Commented:
In the contructor for FooList you must set the FooOptions object to an instance of the object using the new keyword.
Dirk HaestProject managerCommented:
   public class FooList
    {
        public int ID { get; set; }
        public List< FooOptions > ListOfFoos { get; set; }
       public FooList()
      {
          ListOfFoos = new List< FooOptions >
      }

    }


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.