Second parent MDI form: Application.run(aForm) causing null reference exception after close and restart

-1

I am creating a form in a windows application and then running it with the line:

Application.Run(aForm)

When I run this line I get the following error:

System.NullReferenceException: 'Object reference not set to an instance of an object.'

Here is the stack trace:

System.NullReferenceException occurred
  HResult=0x80004003
  Message=Object reference not set to an instance of an object.
  Source=System.Windows.Forms
  StackTrace:
   at System.Windows.Forms.TabControl.get_SelectedTabInternal()
   at System.Windows.Forms.TabControl.OnEnter(EventArgs e)
   at System.Windows.Forms.Control.NotifyEnter()
   at System.Windows.Forms.ContainerControl.UpdateFocusedControl()
   at System.Windows.Forms.ContainerControl.AssignActiveControlInternal(Control value)
   at System.Windows.Forms.ContainerControl.ActivateControlInternal(Control control, Boolean originator)
   at System.Windows.Forms.ContainerControl.SetActiveControlInternal(Control value)
   at System.Windows.Forms.ContainerControl.set_ActiveControl(Control value)
   at System.Windows.Forms.Control.Select(Boolean directed, Boolean forward)
   at System.Windows.Forms.Control.SelectNextControl(Control ctl, Boolean forward, Boolean tabStopOnly, Boolean nested, Boolean wrap)
   at System.Windows.Forms.ContainerControl.Select(Boolean directed, Boolean forward)
   at System.Windows.Forms.Control.SelectNextControl(Control ctl, Boolean forward, Boolean tabStopOnly, Boolean nested, Boolean wrap)
   at System.Windows.Forms.Form.set_Active(Boolean value)
   at System.Windows.Forms.Form.ActivateMdiChildInternal(Form form)
   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.DefMDIChildProc(IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at System.Windows.Forms.Form.DefWndProc(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(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)

Can someone help me to find out what might be causing this? Can I debug this in some way? I call the run method and the program just crashes.

   Private Sub AMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AMenuItem.Click
        Try
            Dim thread As New Thread(New ThreadStart(AddressOf CreateAForm))
            thread.SetApartmentState(ApartmentState.STA)
            thread.Start()
        Catch ex As Exception
            LogAndDisplayException("Error showing a Form. ", ex)
        End Try
    End Sub

Private Sub CreateAForm()


    Dim deviceController = Framework.GetHardware(Of DeviceControllerBase)("Device", True) '.GetHardware < DeviceControllerBase > ("Device", True);
    Dim deviceViewManager As DeviceViewManager = DeviceViewManager.Instance



    Dim aForm As MDIMainForm = New MDIMainForm(deviceController, deviceViewManager)
    Dim aFile = IO.Path.GetFullPath(deviceController.DevConfig.ConfigurationFilepath)

    deviceViewManager.SetDeviceController(deviceController)
    dcafForm.IsMdiContainer = True
    deviceViewManager.SetParentMdiGUI(aForm)
    deviceViewManager.LoadConfigWithinApplication(aFile)
    AddHandler aForm.FormClosing, AddressOf DisposeViews

    Application.Run(aForm)


End Sub

Edit: I should point out that the first time I run the form, it works fine. I then close the form and the second time I run the form, I get this error. I have given more details of the createAForm method.

Edit2: By running the second MDI parent on the main thread, I get past this exception. I think this bug is solved.

vb.net
winforms
mdi
mdiparent
asked on Stack Overflow Dec 18, 2018 by user442920 • edited Dec 18, 2018 by user442920

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0