奇怪的 IndexOutOfRangeException,即使捕获时也是如此
好吧,我有一个公共静态常量:
public static ChatLine[] chatLine = new ChatLine[numChatLines];
。
调试向我显示了这段代码(稍后在同一个文件中):
for (int num12 = 0; num12 < numChatLines; num12++)
{
chatLine[num12] = new ChatLine();
}
将鼠标悬停在每个数据点上时,它向我显示 num12 是 0,而 chatLine 是 chatLine[0]。这很奇怪,因为我的公共常量正如我上面向您展示的那样......知道为什么会发生这种情况吗?
。 。
完整的堆栈跟踪如下:
System.IndexOutOfRangeException was unhandled
Message=Index was outside the bounds of the array.
Source=Project1
StackTrace:
at Project1.Main.Initialize() in C:\Users\X\My Documents\Project1\Main.cs:line 7590
at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun)
at Microsoft.Xna.Framework.Game.Run()
at Project1.Program.Main(String[] args) in C:\Users\X\My Documents\Project1\Program.cs:line 14
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
at System.Activator.CreateInstance(ActivationContext activationContext)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
Well, I have a public static const of:
public static ChatLine[] chatLine = new ChatLine[numChatLines];
.
The debug shows me this code (later in the same file):
for (int num12 = 0; num12 < numChatLines; num12++)
{
chatLine[num12] = new ChatLine();
}
Upon mousing over each data point, it shows me that num12 is 0 and chatLine is chatLine[0]. Which is very odd, since my public const is as I showed you above... Any idea why this is happening?
.
.
The full stack trace is below:
System.IndexOutOfRangeException was unhandled
Message=Index was outside the bounds of the array.
Source=Project1
StackTrace:
at Project1.Main.Initialize() in C:\Users\X\My Documents\Project1\Main.cs:line 7590
at Microsoft.Xna.Framework.Game.RunGame(Boolean useBlockingRun)
at Microsoft.Xna.Framework.Game.Run()
at Project1.Program.Main(String[] args) in C:\Users\X\My Documents\Project1\Program.cs:line 14
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.Runtime.Hosting.ManifestRunner.Run(Boolean checkAptModel)
at System.Runtime.Hosting.ManifestRunner.ExecuteAsAssembly()
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext, String[] activationCustomData)
at System.Runtime.Hosting.ApplicationActivator.CreateInstance(ActivationContext activationContext)
at System.Activator.CreateInstance(ActivationContext activationContext)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssemblyDebugInZone()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于
chatLine
的长度为零,因此在创建数组时numChatLines
也为零。您应该在设置numChatLines
后创建数组。As the length of
chatLine
is zero,numChatLines
is zero when the array is created. You should create the array after settingnumChatLines
.这可能是因为 numChatLines 在
.. 初始化之后被赋予了一个值,这将给出一个 0 值。
尝试:
This is probably because numChatLines is given a value after
.. is initialised, which would give a 0 value.
Try: