gpt4 book ai didi

c# - IEnumerator 给出空异常,但不为空

转载 作者:太空宇宙 更新时间:2023-11-03 20:18:55 26 4
gpt4 key购买 nike

我是 .NET 的新手,所以请多多包涵。

LinqToTwitter 中的 IEnumerable 有一个奇怪的问题。
查询返回一个 IEnumerable,一个 Console.WriteLine 显示它有两个帖子。
但是,当我尝试在枚举器上调用 MoveNext() 时,出现空指针异常。


代码

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

感谢帮助


堆栈跟踪

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

最佳答案

您的 Console.WriteLine 输出表明 Enumerator 是 [Status,Status] 类型的泛型,而不是它包含两个元素。您收到的异常是执行初始查询的结果,而不是像您想象的那样循环遍历枚举器。如果您要将状态分配更改为:

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

您会看到异常现在发生在赋值行,而不是 MoveNext() 行。

我很欣赏这并没有告诉你为什么你会得到你得到的异常,这可能是你的 ctx 实例的 Status 集合映射或填充失败的结果,但希望它能帮助你进度调试。

干杯

关于c# - IEnumerator 给出空异常,但不为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14933465/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com