0

First of all; I do know what a NullPointerException is. I am well aware of What is a NullPointerException, and how do I fix it?.

In my WinForms application I am getting a NullReferenceException at the Application.Exit call with the following Stacktrace:

System.NullReferenceException occured. HResult=0x80004003 Message = The Object-Reference was not set to an instance of the object Source = Microsoft.VisualBasic StackMonitoring: at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.MainFormLoadingDone(Object sender, EventArgs e) at System.EventHandler.Invoke(Object sender, EventArgs e) at System.Windows.Forms.Form.OnLoad(EventArgs e) at System.Windows.Forms.Form.OnCreateControl() at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl() at System.Windows.Forms.Control.WmShowWindow(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.ScrollableControl.WndProc(Message& m) at System.Windows.Forms.Form.WmShowWindow(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)

Originally the message was german due my VS17 beeing german, which I will definitly change to english later on. I translated the few german parts to english.

Also my VS17 shows me Screenshot of Visual Studio where it says

Application is on Hold
Your application was paused, but there is no code to show, because all threads executed external code (usually system- or frameworkcode).

After some research I found out, that most times the error occured on Form Closing and Form Closed events. Since I have not created any closing events myself I do not think that I will find my solution on those.

Calling Environment.Exit(1) instead of Application.Exit() does work, but since this one basicly just kills the process I would like to not use it.

Do you guys have any ideas where to look for the error?


Exception was probably due to the following:

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    If Environment.GetCommandLineArgs.Contains("Confirmation") Then
       Dim a As New confirmationForm
       a.ShowDialog()
       Application.Exit()
    End If
End Sub

There was also an Application.Exit() Call on a.Button1_Press() event. This probably caused the exception. Will update as soon as I have fixed it.

Solution can be found in the answer + comments

Luke
  • 751
  • 2
  • 7
  • 32
  • 1
    It appears to happen in/after a `Form.Load` event. Do you call `Application.Exit()` at application startup/when you open some new form? – Visual Vincent Jul 19 '17 at 08:44
  • Good point. Somehow I actually do call it on a formload event. If the application starts with a parameter it opens the mainform and then on formload opens a childform that will call Application.Exit() on a button press. If you know more for that case let me know. I will now try to debug inside my parents form load event. First I will move my form Load code to open the child form to the form shown event – Luke Jul 19 '17 at 08:46
  • 1
    But does _**the `Load` event itself**_ call `Application.Exit()`, or is it only called if you _press a button_? If the latter then it's not relevant. – Visual Vincent Jul 19 '17 at 08:48
  • I'll say it does, since the child is called using Form.ShowDialog() it will wait for that dialog. If I call Application.Exit() inside that Dialog-Form it will call the Application.Exit() inside the Load event. I will update my question with the relevant code. – Luke Jul 19 '17 at 08:49
  • Actually it's even worse. Some stupid coding mistake I did there! I am calling Form.ShowDialog() and on the next line Application.Exit(). Also I am calling Application.Exit() inside that Dialog-Form which will most likely cause the exception. – Luke Jul 19 '17 at 08:51
  • 1
    It will not literally call `Application.Exit()` _**in the event**_, but since you use `ShowDialog()` it will at least be called before the event is finished, so yeah it is causing the problem. – Visual Vincent Jul 19 '17 at 08:52
  • Updated question. Problem is solved. Thank you for your massive help, basicly I was lost before. Feel free to post an answer on the question for the additional rep if you want to. A (probably) last question; Would you say calling the lines in the question at the Form.Shown() event is fine? Where would you put those lines and why? – Luke Jul 19 '17 at 09:03
  • I guess it's fine. Though I prefer the `Startup` event since that is raised before any form is shown (see answer). – Visual Vincent Jul 19 '17 at 09:11

1 Answers1

1

If you find yourself needing to open another form than the main one I'd rather utilize the application's Startup event for that. It is raised before the main form is shown.

Calling Environment.Exit() after that is harmless. Just make sure that all your code has finished executing so it doesn't get interrupted for instance while writing to files.

If you really don't want to call Environment.Exit() you can try Application.Exit() as well, but I cannot say exactly what will happen if you try to do it in the Startup event (i.e. whether it will still attempt to show the main form or not).

Private Sub MyApplication_Startup(ByVal sender As Object, ByVal e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
    If Environment.GetCommandLineArgs.Contains("Confirmation") Then
        Dim a As New confirmationForm
        a.ShowDialog()
        Environment.Exit(0) '0 = Terminated with no error (success).
    End If
End Sub

EDIT:

It turns out you can actually stop the loading of the main form by setting e.Cancel = True in the Startup event, so that's a better alternative to Environment.Exit().

If Environment.GetCommandLineArgs.Contains("Confirmation") Then
    Dim a As New confirmationForm
    a.ShowDialog()
    e.Cancel = True 'Do not show the main form.
End If
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • Just for the record; I did know that 1 in `Environment.Exit()` stands for an error. I put it in there since it was only used as a workaround for an error. Your code seems nice. I am wondering where exactly to put that sub. Putting it into the mainforms class seems wrong. Perfect would be to achieve an If/Else before any form is created. I know Visual studio generates code "application.run(new mainform())" when you set a startform. But I don't think Visual Studio wants me to edit those generated classes. – Luke Jul 19 '17 at 09:17
  • @Luke : Check the [**linked documentation**](https://msdn.microsoft.com/en-us/library/microsoft.visualbasic.applicationservices.windowsformsapplicationbase.startup(v=vs.110).aspx), it describes how you access the event (see Remarks). – Visual Vincent Jul 19 '17 at 09:18
  • My bad. Great addition to the answers you posted in the comments earlier. Thank you! – Luke Jul 19 '17 at 09:20
  • @Luke : No problem, glad I could help! -- Make sure to check out my edit as well. – Visual Vincent Jul 19 '17 at 09:22