Why would dropping a filled combo box cause the form to crash?

I have a couple of comboboxes filled in like this:

private void PopulateDeptCombox()
{
    try
    {
        ExceptionLoggingService.Instance.WriteLog("Reached FrmInventory.PopulateDeptCombox");
        List<String> depts = hhsdbutils.GetDeptItems();

        foreach (String dept in depts)
        {
            comboBoxsDept.Items.Add(dept);
            comboBoxeDept.Items.Add(dept);
        }

        if (comboBoxsDept.Items.Count > 0)
        {
            comboBoxsDept.SelectedIndex = 0;
        }
        if (comboBoxeDept.Items.Count > 0)
        {
            comboBoxeDept.SelectedIndex = comboBoxeDept.Items.Count - 1;
        }
    }
    catch (Exception ex)
    {
        String msgInnerExAndStackTrace = String.Format(
                "{0}; Inner Ex: {1}; Stack Trace: {2}", ex.Message, ex.InnerException, ex.StackTrace);
        ExceptionLoggingService.Instance.WriteLog(String.Format("From FrmInventory.PopulateDeptCombox(): {0}", msgInnerExAndStackTrace));
    }
}

      

GetDeptItems () just returns multiple items:

List<string> IHHSDBUtils.GetDeptItems()
{
    ExceptionLoggingService.Instance.WriteLog("Reached SQliteHHSDBUtils.GetDeptItems");
    List<String> depts = new List<String> { "dept 1", "dept 2", "dept 3", "Test dept" };
    // TODO: Replace this test data
    return depts;
}

      

This form crashed when I first opened it until I included PopulateDeptCombox () in the try..catch block. It won't crash right away anymore, but it still writes NRE to the log file:

Date: 3/21/2009 1:41:27 AM
Message: Reached FrmInventory.PopulateDeptCombox

Date: 3/21/2009 1:41:27 AM
Message: From FrmInventory.PopulateDeptCombox(): NullReferenceException; Inner Ex: ; Stack Trace:    at 

HHS.FrmInventory.PopulateDeptCombox()
   at HHS.FrmInventory..ctor(String inventoryName, String siteNum, Boolean allowNewItems)
   at HHS.frmNewInventory.buttonOK_Click(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.ButtonBase.WnProc(WM wm, Int32 wParam, Int32 lParam)
   at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
   at Microsoft.AGL.Forms.EVL.EnterModalDialog(IntPtr hwnModal)
   at System.Windows.Forms.Form.ShowDialog()
   at HHS.frmMain.menuItemFILE_NewInventory_Click(Object sender, EventArgs e)
   at System.Windows.Forms.MenuItem.OnClick(EventArgs e)
   at System.Windows.Forms.Menu.ProcessMnuProc(Control ctlThis, WM wm, Int32 wParam, Int32 lParam)
   at System.Windows.Forms.Form.WnProc(WM wm, Int32 wParam, Int32 lParam)
   at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
   at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
   at System.Windows.Forms.Application.Run(Form fm)
   at HHS.Program.Main()

      

... but ignores the NRE and the soldier (acts like the NRE doesn't notice).

Then if I hit "comboBoxsDept" to check what items it contains, it first hourglass for the spell, then it crashes showing the objects I expect to see - but only for the eye mark - then the form crashes. "Post mortem" (log file) shows the NRE above, plus this at the end of the log file:

Date: 3/21/2009 1:42:08 AM
Message: Reached FrmInventory.BarcodeReader_ReadNotify

Date: 3/21/2009 1:42:08 AM
Message: From application-wide exception handler: System.NullReferenceException: NullReferenceException
   at HHS.FrmInventory.BarcodeReader_ReadNotify(Object sender, EventArgs e)
   at System.Windows.Forms.Control.TASK.Invoke()
   at System.Windows.Forms.Control._InvokeAll()
   at System.Windows.Forms.Control.WnProc(WM wm, Int32 wParam, Int32 lParam)
   at System.Windows.Forms.Control._InternalWnProc(WM wm, Int32 wParam, Int32 lParam)
   at Microsoft.AGL.Forms.EVL.EnterMainLoop(IntPtr hwnMain)
   at System.Windows.Forms.Application.Run(Form fm)
   at HHS.Program.Main()

      

So there is another NRE related to barcode scan code; for some reason this code is NRE-crazy. I have other forms with the same barcode scan setup code, but they don't work like this. Everything is fine until I scan the value and then drop out comboBoxsDept.

How can a populated combo box reveal an NRE?

UPDATE

If I scan something into the barcode textbox first, prevent crashing. If I go straight to the dropdown it works. So it appears to be some kind of weirdness / shattering with the barcode scan code.

By the way, this was my last question of the year; don't drink too much spicy V8 today.

UPDATE 2

Answer to Grant Winnie: Here is my overloaded constructor (the one that gets called):

public FrmInventory(string inventoryName, string siteNum, bool allowNewItems)
{
    ExceptionLoggingService.Instance.WriteLog("Reached FrmInventory overloaded constructor");
    InitializeComponent();

    _siteNum = siteNum;
    _allowNewItems = allowNewItems;
    _invName = inventoryName;
    invFileName = Path.GetFileNameWithoutExtension(HHSUtils.GetGeneratedINVFileName(_siteNum));
    recordDate = HHSUtils.ConvertDateTimeToSQLiteFormat(DateTime.Now);
    comboBoxUPC_PLU.SelectedIndex = 0;
    HookupHandlers();
    PopulateDeptCombox();
    PopulateSubdeptCombox();
    SetRequiredControls();
    textBoxUPC_PLU.Focus();
}

      

UPDATE 3

This turned out to be a "culvert":

private void textBoxUPC_LostFocus(object sender, EventArgs e)
{
    ExceptionLoggingService.Instance.WriteLog("Reached frmVerify.textBoxUPC_LostFocus");
    this.DisposeBarcodeReaderAndData();
}

private void DisposeBarcodeReaderAndData()
{
    ExceptionLoggingService.Instance.WriteLog("Reached 
FrmInventory.DisposeBarcodeReaderAndData");
    // If we have a reader
    if (this.barcodeReader != null)
    {
        // Disable the reader
        this.barcodeReader.Actions.Disable();
        // Free it up
        this.barcodeReader.Dispose();
        // Indicate we no longer have one
        this.barcodeReader = null;
    }

    // If we have a reader data
    if (this.barcodeReaderData != null)
    {
        // Free it up
        this.barcodeReaderData.Dispose();
        // Indicate we no longer have one
        this.barcodeReaderData = null;
    }
}

      

As soon as I commented out the call to DisposeBarcodeReaderAndData () - creating this dead method code - the sun came out, the birds started singing, they hung up a dash that came up with a job and falling down didn't crash anymore.

UPDATE 4

The problem is back, dad! I had to move some of my constructor code to the Load () form to make it straighten and fly to the right.

+3


source to share





All Articles