Value cannot be null.Parameter name: source

edx
edx
Member
506 Points
24 Posts

Endpoint throwing argument null exception as:

Exception:
System.ArgumentNullException: Value cannot be null.
Parameter name: source
   at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Boolean& found)
   at System.Linq.Enumerable.FirstOrDefault[TSource](IEnumerable`1 source)

When I investigated then found that it's coming from Linq extension method:

_service.GetDoc().FirstOrDefault();

Where GetDoc() as:

 public IEnumerable<Doc> GetDoc()
{
    //
}
Views: 25353
Total Answered: 1
Total Marked As Answer: 1
Posted On: 13-Apr-2020 23:48

Share:   fb twitter linkedin
Answers
Smith
Smith
None
2568 Points
74 Posts
         

You are right. Every extension methods from System.Linq that takes Reference types will check if they are null if(source== null) and throw a ArgumentNullException if they are. 

In case of your, following service method returning run and causing issue:

_service.GetDoc()

Please check for null or use null-able operator as:

var result = _service.GetDoc()?.FirstOrDefault();

if(result!=null){
  //do whatever
}
Posted On: 15-Apr-2020 23:20
 Log In to Chat