【发布时间】:2009-03-20 12:54:59
【问题描述】:
当我尝试运行以下代码时,它会导致未处理的异常。在对代码进行了多次修改后,我发现如果注释掉 MessageBox.Show 行,问题就会消失!不同寻常的是,我在代码的其他部分的其他 catch{ } 段中使用了 MessageBox.Show 语句,没有任何问题。我的问题是有人知道它为什么会导致异常吗?
(P.s Reports_Group_Chooser 是一个组合框)
代码:
string GroupName= (string)Reports_Group_Chooser.SelectedItem;
byte[] ConfigBytes= new byte[]{};
try{
ConfigBytes= File.ReadAllBytes("Reports/"+ GroupName.ToLower() +".grp");
}catch{
MessageBox.Show("The file for this group is missing. Cannot continue.","File Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
Reports_Group_Chooser.Items.RemoveAt(NewGroup);
Reports_Group_Chooser.SelectedIndex= 0;
}
错误(大部分):
未处理的异常: System.NullReferenceException:对象 引用未设置为 对象在 System.Windows.Forms.ComboBox.DropDownListBoxFinished () [0x00000] 在 (包装 远程调用检查) System.Windows.Forms.ComboBox:DropDownListBoxFinished () 在 System.Windows.Forms.ComboBox+ComboListBox.HideWindow () [0x00000] 在 System.Windows.Forms.ComboBox+ComboListBox.OnMouseUp (System.Windows.Forms.MouseEventArgs e) [0x00000] 在 System.Windows.Forms.Control.WmLButtonUp (System.Windows.Forms.Message&m) [0x00000] 在 System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message&m) [0x00000] 在 System.Windows.Forms.ComboBox+ComboListBox.WndProc (System.Windows.Forms.Message&m) [0x00000] 在 System.Windows.Forms.Control+ControlWindowTarget.OnMessage (System.Windows.Forms.Message&m) [0x00000] 在 System.Windows.Forms.Control+ControlNativeWindow.WndProc (System.Windows.Forms.Message&m) [0x00000] 在 System.Windows.Forms.NativeWindow.WndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) [0x00000] 在 System.Windows.Forms.XplatUIX11.DispatchMessage (System.Windows.Forms.MSG&味精) [0x00000] 在 System.Windows.Forms.XplatUI.DispatchMessage (System.Windows.Forms.MSG&味精) [0x00000] 在 System.Windows.Forms.Application.RunLoop (布尔模态, System.Windows.Forms.ApplicationContext 上下文)[0x00000]
任何帮助表示赞赏 迈克尔
更新 这是一个在我的代码中工作的 MessageBox.Show 示例,它不会导致错误:
GlobalConfig= new Dictionary<string, string>();
byte[] ConfigBytes= new byte[]{};
try{
ConfigBytes= System.IO.File.ReadAllBytes("Config.cfg");
}catch{
MessageBox.Show("Global ettings file does not exist. Cannot continue.","File Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
GlobalConfig.Add("StoreNumber","");
GlobalConfig.Add("Error","Y");
}
更新更新:
似乎问题只是在组合框事件中有 MessageBox.Show: 以下代码仍然出现同样的错误:
private void Reports_GroupChanged(object sender,EventArgs e){
MessageBox.Show("The file for this group is missing. Cannot continue.","File Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
【问题讨论】:
-
您的错误是组合框而不是消息框。问题是调用堆栈不包含行号。这是在 WPF 中吗? APS.NET? Windows 论坛?
-
抱歉,我对 C# 编程非常陌生,您的意思是“这是在 WPF 中吗?APS.NET?Windows 论坛?”
标签: c# try-catch unhandled-exception