【问题标题】:Index out of range error, when it isnt索引超出范围错误,如果不是
【发布时间】:2013-03-11 19:31:26
【问题描述】:

我有以下方法:

private void DetermineIfWagerIsValid(CouponViewModel result, Bet bet, string wagerType, int selectionCount)
{
    if (bet.Wagers[0].WagerType == wagerType) //error here
    {
        if (bet.Selections.Count != selectionCount)
        {
            bet.BetStatus = BetStatus.FilledInAndInvalid;
        }
    }
}

很简单,但是当索引似乎没有超出范围时,我会收到间歇性的“索引超出范围”错误:

这是 StackTrace:

在 System.ThrowHelper.ThrowArgumentOutOfRangeException() 在 System.Collections.Generic.List1.get_Item(Int32 index) at System.Collections.ObjectModel.Collection1.get_Item(Int32 索引)
在 Arkle.CouponProcessing.Scan.LonglistDecoder_994550.DetermineIfWagerIsValid(CouponViewModel 结果, Bet bet, String betType, Int32 selectionCount) in c:\code\Arkle\Arkle\Arkle.CouponProcessing\Scan\LonglistDecoder_994550.cs:line 117 在 Arkle.CouponProcessing.Scan.LonglistDecoder_994550.DetermineIfBetIsValid(CouponViewModel 导致 c:\code\Arkle\Arkle\Arkle.CouponProcessing\Scan\LonglistDecoder_994550.cs:line 107 在 Arkle.CouponProcessing.Scan.LonglistDecoder_994550.Decode() 在 c:\code\Arkle\Arkle\Arkle.CouponProcessing\Scan\LonglistDecoder_994550.cs:line 62 在 ArkleWPF.UI.SlipScanning.CouponTools.DecodeCoupon(图像 img, OMRForm omrForm1, CouponDecoder 解码器, CouponPrintingInformation viewSettings, String slipBarcode, DecodeStatus status) in C:\code\Arkle\Arkle\ArkleWPF\UI\SlipScanning\CouponTools.vb:line 215
在 ArkleWPF.UI.SlipScanning.CouponTools.ProcessForm(OMRForm omrForm1, DecodeStatus status, CouponPrintingInformation viewSettings, Boolean 总是乐透)在 C:\code\Arkle\Arkle\ArkleWPF\UI\SlipScanning\CouponTools.vb:89 行
在 ArkleWPF.UI.SlipScanning.CouponTools._Closure$__1._Lambda$__1() 中 C:\code\Arkle\Arkle\ArkleWPF\UI\SlipScanning\CouponTools.vb:line 27
在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态)
在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态)在 System.Threading.ThreadHelper.ThreadStart()

这不是每次都发生,更像是每秒钟或第三次,这让我发疯!有什么想法吗?

【问题讨论】:

  • 我很确定例外总是正确的。
  • @GrantThomas 完全同意,但看看截图,你就会明白我为什么感到困惑!
  • 您是否在某些地方使用多线程来访问 Wagers ?
  • 堆栈跟踪说明了什么? WagerType 是否有可能在其 get 中进行一些索引访问?
  • 查找堆栈跟踪(单击View Detail)或捕获异常并打印堆栈跟踪。目前尚不清楚究竟在哪里引发了异常。

标签: c# arrays


【解决方案1】:
 System.Collections.Generic.List1.get_Item(Int32 index) at
 System.Collections.ObjectModel.Collection1.get_Item(Int32 index)

请求的索引在列表查找中不存在。 Wagers 是一个数组,但 WagerType 没有请求的索引。 exception 是从列表的 get 语句中引发的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 2016-04-17
    • 2015-07-13
    相关资源
    最近更新 更多