title: base.dispose() causing Object Reference Not Set To An Instance Of An Object
body: <p>I have a program that I have been developing over time which has suddenly started throwing errors up when closing forms.</p>
  
  <p>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.</p>
  
  <p>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</p>
  
  <p>This has been working fine since forever but now all of a sudden something is starting to throw up a NRE.</p>
  
  <pre><code>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&amp; m)
  at System.Windows.Forms.Form.WndProc(Message&amp; m)
  at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
  at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; 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&amp; m)
  at System.Windows.Forms.Control.DefWndProc(Message&amp; m)
  at System.Windows.Forms.Control.WndProc(Message&amp; m)
  at System.Windows.Forms.MdiClient.WndProc(Message&amp; m)
  at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message&amp; m)
  at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message&amp; m)
  at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
  </code></pre>
  
  <p>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.</p>
  
  <p>Following Code launch's the forms.</p>
  
  <pre><code>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();
          }
  
      }
  </code></pre>
  
  <p>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.</p>
  
  <p>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.</p>
  
  <p>My main form calls the class as follows</p>
  
  <pre><code>                 SummaryFormLaunch SFL = new SummaryFormLaunch();
                  SFL.CheckIfExisits(this, cmbDBASE.Text, cmbODBC.Text, ultraToolbarsManager1);
  </code></pre>
  
  <p>Any Advice on trying to trace down the cause of the error would be welcome as i am at a loss.</p>
  
  <p>Cheers!</p>
  
  <p>EDit: Using VS 2008, .net 3.5SP1</p>
  
  <p>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.</p>
  
  <p>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.</p>