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 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
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
User contributions licensed under CC BY-SA 3.0