开发者

Odd IndexOutOfRangeException, even when caught

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 happe开发者_运维百科ning?

. .

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: 


As the length of chatLine is zero, numChatLines is zero when the array is created. You should create the array after setting 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];
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜