0

I have a class:

public class ClientModelData
{
    public int clientID { get; set; }
    public IList<int> LocationIDs { get; set; }
}

When I call it:

ClientModelData obj = new ClientModelData();
obj.LocationIDs.Add(1);

It throws an exception:

`((System.Collections.Generic.ICollection<int>)(client.LocationID))' is null`
Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
Sohail
  • 45
  • 1
  • 6
  • Create constructor in Class and re-initiate property with new Keyword; LocationIDs = new List(); // where T can be any class reference. – Sohail Nov 05 '15 at 15:58

2 Answers2

9

LocationIDs is not initialized therefore it is giving you the error.

public IList<int> LocationIDs { get; set; }

You should create an instance in the constructor

public ClientModelData()
{
  LocationIDs = new List<int>();
}
Asif Mushtaq
  • 13,010
  • 3
  • 33
  • 42
3

You should initialize your list with actual object, for example in the constructor. Add this to your class:

public ClientModelData()
{
   LocationIDs = new List<int>();
}
JleruOHeP
  • 10,106
  • 3
  • 45
  • 71