Add the initialization of the internal list in the constructor of your class
public class MapLocationCompany
{
[Key]
public int id { get; set; }
[HiddenInputAttribute]
public string UserName { get; set; }
public string CompanyName { get; set; }
public double MapStartupLatitude { get; set; }
public double MapStartupLongitude { get; set; }
public int MapInitialZoomIn { get; set; }
public List<MapLocation> MapLocationList { get; set; }
public void MapLocationCompany()
{
MapLocationList = new List<MapLocation>();
}
}
We can't see what is the code in getMapLocationCompany(id);
, but I suppose that it creates, in some way, an instance of the class MapLocationCompany
and returns this instance. But in the default situation this new instance has its property MapLocationList set to null and thus, if you try to use that property (.ToList()
) you get a Null Reference Exception. Adding the code above to the constructor helps to avoid this problem. The List is still empty and you need to fill it but the internal variable that keeps the list is initialized and you can use it to add new elements.
As a final note, the error is caused by your reference to ToList(), but MapLocationList
is already defined as a List so you could remove it.