次のコードを実行しようとすると、未処理の例外が発生します。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] でオブジェクトのインスタンスに設定されていません (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) System.Windows.Forms.Control.WndProc で [0x00000] (System.Windows.Forms.Message& m) System.Windows.Forms.ComboBox で [0x00000] +ComboListBox.WndProc (System.Windows.Forms.Message& m) System.Windows.Forms.Control+ControlWindowTarget で [0x00000]。OnMessage (System.Windows.Forms.Message& m) System.Windows.Forms.Control+ControlNativeWindow.WndProc で [0x00000] (System.Windows.Forms.Message& m) System.Windows.Forms.NativeWindow.WndProc で [0x00000] ( 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 (システム.Windows.Forms.MSG& msg) [0x00000] System.Windows.Forms.Application.RunLoop (ブール モーダル、System.Windows.Forms.ApplicationContext コンテキスト) [0x00000] でWndProc (IntPtr hWnd、Msg msg、IntPtr wParam、IntPtr lParam) System.Windows.Forms.XplatUIX11.DispatchMessage で [0x00000] (System.Windows.Forms.MSG& msg) System.Windows.Forms.XplatUI.DispatchMessage で [0x00000] (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) System.Windows.Forms.XplatUIX11.DispatchMessage で [0x00000] (System.Windows.Forms.MSG& msg) System.Windows.Forms.XplatUI.DispatchMessage で [0x00000] (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);
}