1

次のコードを実行しようとすると、未処理の例外が発生します。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);
}
4

3 に答える 3

0

最初にエラーを修正します。

Reports_Group_Chooser.SelectedIndex= 0;
Reports_Group_Chooser.Items.RemoveAt(NewGroup);    
MessageBox.Show("The file for this group is missing. Cannot continue.","File Error",MessageBoxButtons.OK, MessageBoxIcon.Exclamation);    
于 2009-03-20T13:53:17.680 に答える
0

メッセージを表示する前に、失敗の原因となった問題を修正する必要があります。

以下は私の例です。

この失敗は、null オブジェクトを文字列に変換したことが原因でした:

string str = dgv[e.ColumnIndex, e.RowIndex].Value.ToString();

次に、catch ステートメントでメッセージを表示し、以前の値をセルに割り当てようとしました。

MessageBox.Show(String.Format("Value must be between {0} and {1}.", minVal, maxVal));
dgv[e.ColumnIndex, e.RowIndex].Value = previousValue;

MessageBox の呼び出し中に null 参照例外が発生しました。

そのため、MessageBox を呼び出す前にセルの値を修正する必要があり (行を入れ替える)、魅力的に機能しました。

于 2013-07-11T13:30:40.103 に答える