问题描述
||
好吧,我有一个公共静态常量:
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,Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException:
解决方法
由于
chatLine
的长度为零,因此创建数组时the4ѭ的长度为零。设置numChatLines
后应创建数组。
,这可能是因为numChatLines在
public static ChatLine[] chatLine = new ChatLine[numChatLines];
..被初始化,将给出一个0值。
尝试:
public static ChatLine[] chatLine;
void main()
{
/* ... your code ... */
numChatLines = 12;
chatLine = new ChatLine[numChatLines];
}