Makinde A. Israel

Makinde A. Israel

  • 1.6k
  • 168
  • 20.6k

Yield return throws object reference not set exception

Jun 12 2018 9:22 PM
Good day
 
I have been trying to use a linq to entity SelectMany() but always throw "object reference not set to an instance of an object" exception. thougth may be one of the elements of the sequence was null. Therefore i try to extend(write an extension method) for the SelectMany() as SelectManyOrDefault() to check is there is any null element and skip such but i was still getting the same  "object reference not set to an instance of an object" exception. Try to set break points debug my project but to my surprise none of the elements at any point is null or empty. The exception is been thrown on the "yeild return item". This is the extension method
 
  1. /// <Summary>  
  2.         /// Projects each element of a sequence to an System.Collections.Generic.IEnumerable<TResut>  
  3.         /// and flattens the resulting sequences into one sequence.  
  4.         /// Or returns a default value if the sequence contains no elements.  
  5.         /// <param name="source">A sequence of values to project.</param>  
  6.         /// <param name="selector">A transform function to apply to each element.</param>  
  7.         /// <typeparam name="TS">The type of the elements of source.</typeparam>  
  8.         /// <typeparam name="TR">The type of the elements of the sequence returned by selector.</typeparam>  
  9.         /// <returns>An System.Collections.Generic.IEnumerable`1 whose elements are the result of  
  10.         /// invoking the one-to-many transform function on each element of the input sequence.</returns>  
  11.         /// <exceptions>  
  12.         /// System.ArgumentNullException:  
  13.         /// source or selector is null.  
  14.         /// </exceptions>  
  15.         /// </Summary>  
  16.         public static IEnumerable<TR> SelectManyOrDefault<TS, TR>(this IEnumerable<TS> source, Func<TS, IEnumerable<TR>> selector)  
  17.         {  
  18.             if (source == null)  
  19.                 throw new ArgumentNullException("source");  
  20.   
  21.             if (selector == null)  
  22.                 throw new ArgumentNullException("selector");  
  23.   
  24.             //if (source.Count() <= 0)  
  25.             //return default(IEnumerable<TR>);  
  26.   
  27.             //if (source.Count() == 1)  
  28.             //    return source.Select(selector).FirstOrDefault();  
  29.   
  30.             foreach (TS element in source.DefaultIfEmpty())  
  31.             {  
  32.                 if (element != null)  
  33.                 {  
  34.                     foreach (TR item in selector(element).DefaultIfEmpty())  
  35.                     {  
  36.                         if(item != null)  
  37.                             yield return item;  
  38.                     }  
  39.                 }  
  40.             }  
  41.   
  42.             //return result;  
  43.         } 
 
Please help me, I don't know what is actually cause this exception.
I have been on this for a week now.