base.dispose() causing Object Reference Not Set To An Instance Of An Object

2.6k views Asked by At

I have a program that I have been developing over time which has suddenly started throwing errors up when closing forms.

Normally when you debug the software if it throws up one of these errors you get taken to the code and you can find what the issue is, however for some reason this error does show in the debugger, through stepping through the code i have managed to narrow it down to when its calling the base.Dispose() on the form its closing, and only if its closed in a certain order.

I have a Main form, which is populated with Mdichildren forms every time the user clicks connect, it will dispose all the forms and re-create them as tabs

This has been working fine since forever but now all of a sudden something is starting to throw up a NRE.

System.NullReferenceException: Object reference not set to an instance of an object.
at System.Windows.Forms.Form.DeactivateMdiChild()
at System.Windows.Forms.Form.WmMdiActivate(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg , IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.MdiClient.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

I can see its something to do with the Mdi child form deactivating, I have Checked the Parent form is set correctly which it is and am now lost after lots of googling.

Following Code launch's the forms.

public void CheckIfExisits(Form MainForm, string DBASE, string ODBC, UltraToolbarsManager UTBM)
    {
        var ASQL = new Accounts.SQL.SummaryTab();
        var FSQL = new FocalPoint.SQL.SummaryTab();
        var WSQL = new WorkFlowForms.SQL.SummaryTab();

        var AVER = ASQL.DimVer(DBASE, ODBC);
        var FVER = FSQL.FPVer(DBASE, ODBC);
        var WVER = WSQL.WffVer(DBASE, ODBC);

        if (AVER != "0")
        {
            var FrmAccSum = new FrmAccountsSummary(DBASE, ODBC, UTBM) { MdiParent = MainForm };
            FrmAccSum.Show();
        }

        if (FVER != "0")
        {
            var FrmFPSum = new FrmFocalPointSummaryNew(DBASE, ODBC, UTBM) { MdiParent = MainForm };
            FrmFPSum.Show();
        }

        if (WVER != "0")
        {
            var FrmWFFSum = new FrmWorkFlowFormSummarys(DBASE, ODBC, UTBM) { MdiParent = MainForm };
            FrmWFFSum.Show();
        }

    }

If i close the forms in reverse order that they were opened it works fine, if however i try close the Accounts form then the Focalpoint form i get the NRE error.

I can provide the code that closes the forms, however as i can replicate this by closing the forms manually i dont think this is where the issue lies.

My main form calls the class as follows

                 SummaryFormLaunch SFL = new SummaryFormLaunch();
                SFL.CheckIfExisits(this, cmbDBASE.Text, cmbODBC.Text, ultraToolbarsManager1);

Any Advice on trying to trace down the cause of the error would be welcome as i am at a loss.

Cheers!

EDit: Using VS 2008, .net 3.5SP1

Edit 2: Will also add that if i turn on Thrown for NRE in the exceptions list i still don't get taken to the error at hand just gives me the NRE Error in a box and doesn't take me to the part in the code.

Edit 3: This appears to happen whenever i close the Accounts form (First mdi child created) first, if i leave this to last it appears to work fine, however if i close this first, whatever the last form closed is will cause the NRE.

1

There are 1 answers

0
Christopher Lewis On

Well this was a nightmare, one that could have been easier to diagnose if I could actually debug it correctly.

I had changed part of the code to make a form active, this was to help set a focus on a form if the header tabs changed (As these should always show the same information)

I had used this

this.ActivateMdiChild(frm);

Now this is fine and does the job i want it too (Make the correct form active) however when the Base.Dispose does it disposing it looks at the Parent form for the ActiveMdiChild and tries to deactivate it.

But because what was the active MdiChild has already been deactivated this is a null value, which was causing my NRE error.

Does mean i have to try and find another way of setting the active form but at least this random issue has now gone!