IluTov
IluTov

Reputation: 6862

IEnumerator gives null exception, but is not null

I'm pretty new to .NET, so please bear with me.

I have a strange issue with an IEnumerable in LinqToTwitter.
The query returns an IEnumerable, and a Console.WriteLine shows that it holds two posts.
However when I try to call MoveNext() on the enumerator, I get a null-pointer exception.


Code

TwitterContext ctx = this.twitterContext;

IEnumerable<Status> statuses =
    from tweet in ctx.Status
        .AsEnumerable()
    select tweet;

IEnumerator<Status> eStat = statuses.GetEnumerator();

// The output is:
// System.Linq.Enumerable+WhereSelectEnumerableIterator`2[LinqToTwitter.Status,LinqToTwitter.Status]
// So this shows that the IEnumerable holds 2 status values
Console.WriteLine(eStat);

// This line gives the exception
// "Value cannot be null."
Boolean hasNext = eStat.MoveNext();

Thanks for the help


Stacktrace

   at System.Reflection.RuntimeMethodInfo.MakeGenericMethod(Type[] methodInstantiation)
   at LinqToTwitter.TwitterQueryProvider.Execute[TResult](Expression expression)
   at LinqToTwitter.TwitterQueryable`1.GetEnumerator()
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Broadcast.TwitterFeed.Program.Main(String[] args) in C:\Daten\TFS-Workspace\GD-TOP\Broadcast\Broadcast.TwitterFeed.Service\Broadcast.TwitterFeed\Program.cs:line 20
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()

Upvotes: 1

Views: 1451

Answers (1)

Rob Epstein
Rob Epstein

Reputation: 1500

Your Console.WriteLine output indicates that the Enumerator is a generic of type [Status,Status], not that it contains two elements. The exception you are receiving is a result of executing the initial query not looping through the enumerator as you may think. If you were to change your statuses assignment to:

IEnumerable<Status> statuses =
(from tweet in ctx.Status
select tweet).ToList();

You will see that the exception now occurs on the assignment line, not the MoveNext() line.

I appreciate that this does not tell you why you're getting the exception you're getting, which is likely a result of a failed mapping or population of the Status collection of your ctx instance, but hopefully it will help you make progress debugging.

Cheers

Upvotes: 5

Related Questions