当我尝试运行以下代码时,它会导致未处理的异常。经过对代码的大量修改后,如果注释掉 MessageBox.Show 行,我发现问题就消失了!不同寻常的是,我在代码的其他部分的其他 catch{ } 段中使用了 MessageBox.Show 语句,没有任何问题。我的问题是有谁知道它为什么会导致异常?
(Ps Reports_Group_Chooser 是一个 ComboBox)
编码:
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] at (wrapper remoting-invoke-with-check) 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 & msg) [0x00000] 在 System.Windows.Forms.XplatUI.DispatchMessage (System .Windows.Forms.MSG& msg) [0x00000] 在 System.Windows.Forms.Application.RunLoop(布尔模式,System.Windows.Forms.ApplicationContext 上下文)[0x00000]WndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) [0x00000] 在 System.Windows.Forms.XplatUIX11.DispatchMessage (System.Windows.Forms.MSG & msg) [0x00000] 在 System.Windows.Forms.XplatUI.DispatchMessage (System.Windows.Forms.MSG& msg)[0x00000] 在 System.Windows.Forms.Application.RunLoop (布尔模式,System.Windows.Forms.ApplicationContext 上下文)[0x00000]WndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) [0x00000] 在 System.Windows.Forms.XplatUIX11.DispatchMessage (System.Windows.Forms.MSG & msg) [0x00000] 在 System.Windows.Forms.XplatUI.DispatchMessage (System.Windows.Forms.MSG& 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);
}