views:

573

answers:

2

Hello, When I try to run the following code It causes an Unhandled Exception. After much tweeking with the code I found if commented out the MessageBox.Show line the problem goes away! Unusually I have used MessageBox.Show statments in other catch{ } segments in other parts of the code with no problems. My question is does anyone know why its causing the exception?

(P.s Reports_Group_Chooser is a ComboBox)

The Code:

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;
}

The error (well most of it):

Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object at System.Windows.Forms.ComboBox.DropDownListBoxFinished () [0x00000] at (wrapper remoting-invoke-with-check) System.Windows.Forms.ComboBox:DropDownListBoxFinished () at System.Windows.Forms.ComboBox+ComboListBox.HideWindow () [0x00000] at System.Windows.Forms.ComboBox+ComboListBox.OnMouseUp (System.Windows.Forms.MouseEventArgs e) [0x00000] at System.Windows.Forms.Control.WmLButtonUp (System.Windows.Forms.Message& m) [0x00000] at System.Windows.Forms.Control.WndProc (System.Windows.Forms.Message& m) [0x00000] at System.Windows.Forms.ComboBox+ComboListBox.WndProc (System.Windows.Forms.Message& m) [0x00000] at System.Windows.Forms.Control+ControlWindowTarget.OnMessage (System.Windows.Forms.Message& m) [0x00000] at System.Windows.Forms.Control+ControlNativeWindow.WndProc (System.Windows.Forms.Message& m) [0x00000] at System.Windows.Forms.NativeWindow.WndProc (IntPtr hWnd, Msg msg, IntPtr wParam, IntPtr lParam) [0x00000] at System.Windows.Forms.XplatUIX11.DispatchMessage (System.Windows.Forms.MSG& msg) [0x00000] at System.Windows.Forms.XplatUI.DispatchMessage (System.Windows.Forms.MSG& msg) [0x00000] at System.Windows.Forms.Application.RunLoop (Boolean Modal, System.Windows.Forms.ApplicationContext context) [0x00000]

Any help appreciated Michael

UPDATE This is an example of a working MessageBox.Show in my code which does not cause an error:

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");
}

Update update:

It seems the problem is just having the MessageBox.Show within a combobox event: The following code still presents the same error:

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);
}
A: 

When you show a MessageBox it does not pause your application. Instead the application continues to pump messages from the operating system. In affect this allows your UI to continue processing.

What's likely happening here is that while the MessageBox is showing, the ComboBox is still processing the mouse button up message and null referencing. Try putting the following call instead.

System.Diagnostics.Debugger.Break();
JaredPar
I put the System.Diagnostics.Debugger.Break(); in the code after the MessageBox and I received no error but I don't want the program to just halt on this error :(.
redorkulated
@redorkulated, it helps narrow down the problem though. Try disabling the ComboBox before showing the message box then.
JaredPar
hmm nope disabled the combobox at the start of the method and still got the same error.
redorkulated
A: 

Fix the errors first.

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);
jyoung
I swapped lines around as you suggested but that made no diffrence. These two lines work fine its when the MessageBox line is uncommented do i receive the error. I also moved MessageBox to its own segment after the catch.if(ConfigBytes.Length == 0){MsgBox...}Still came up with the same error.
redorkulated