简体   繁体   English

IEnumerator给出null异常,但不为null

[英]IEnumerator gives null exception, but is not null

I'm pretty new to .NET, so please bear with me. 我是.NET的新手,请耐心等待。

I have a strange issue with an IEnumerable in LinqToTwitter . 我在LinqToTwitter中有一个IEnumerable的奇怪问题。
The query returns an IEnumerable , and a Console.WriteLine shows that it holds two posts. 该查询返回一个IEnumerable ,并且Console.WriteLine显示它拥有两个帖子。
However when I try to call MoveNext() on the enumerator, I get a null-pointer exception. 但是,当我尝试在枚举数上调用MoveNext()时,出现了空指针异常。


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()

Your Console.WriteLine output indicates that the Enumerator is a generic of type [Status,Status], not that it contains two elements. Console.WriteLine输出表明Enumerator是[Status,Status]类型的泛型,而不是它包含两个元素。 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. 您将看到异常现在出现在赋值行,而不是MoveNext()行。

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. 我很高兴地说,这并不能告诉您为什么会得到异常,这可能是由于ctx实例的Maps实例的映射失败或填充失败而引起的,但是希望它可以帮助您进行调试。

Cheers 干杯

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM