I have created a new empty List of type User (User is an Entity in my EDM)
List<User> AvailableLocums = new List<User>();
AvailableLocums = null;
I also have another List of type User which is populated with the results of a query
List<User> Locums = _shiftDateService.GetAvailableLocums(id, shiftDate.shiftDateID).ToList();
I then wish to loop around the Locums List, do some logic, and then add the User to my AvailableLocums List
foreach (var locum in Locums)
{
//Do some logic
AvailableLocums.Add(locum);
}
However, when I try to do this I get the following error
Object reference not set to an instance of an object.
I then tried to amend my code and do the following
foreach (var locum in Locums)
{
//Do some logic
User locumUser = new User();
locumUser = locum;
AvailableLocums.Add(locumUser);
}
But again I get the same error
Object reference not set to an instance of an object.
Could someone please help me with this?
Thanks.
You are nulling out that list
hence, the exception. You don’t need this line to make an empty list: