奇怪的 IndexOutOfRangeException,即使捕获时也是如此

发布于 2024-11-09 04:43:14 字数 2022 浏览 0 评论 0原文

好吧,我有一个公共静态常量:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

-残月青衣踏尘吟 2024-11-16 04:43:14

由于 chatLine 的长度为零,因此在创建数组时 numChatLines 也为零。您应该在设置numChatLines后创建数组。

As the length of chatLine is zero, numChatLines is zero when the array is created. You should create the array after setting numChatLines.

残龙傲雪 2024-11-16 04:43:14

这可能是因为 numChatLines

public static ChatLine[] chatLine = new ChatLine[numChatLines];

.. 初始化之后被赋予了一个值,这将给出一个 0 值。

尝试:

public static ChatLine[] chatLine;
void main()
{
  /* ... your code ... */
  numChatLines = 12;
  chatLine = new ChatLine[numChatLines];
}

This is probably because numChatLines is given a value after

public static ChatLine[] chatLine = new ChatLine[numChatLines];

.. is initialised, which would give a 0 value.

Try:

public static ChatLine[] chatLine;
void main()
{
  /* ... your code ... */
  numChatLines = 12;
  chatLine = new ChatLine[numChatLines];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文