Form Exit Error (Null Reference) version 1.1

CoachBarker

Well-known member
Joined
Jul 26, 2007
Messages
133
Programming Experience
1-3
I have an app that has a Main Form from which other forms are launched, frmPatientInfo is one. The frmPatientInfo launches another form, frmGlucoseLevels.

VB.NET:
Private Sub btnGlucoseLevels_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGlucoseLevels.Click
        Try
            If frmMainScreen.Glucose Is Nothing Then
                ' let the base know we are opening the form
                Dim ID As Int16 = intPatientID
                frmMainScreen.Glucose = New frmGlucose(oLogin, ID)
                frmMainScreen.Glucose.Show()
            Else
                frmMainScreen.Glucose.BringToFront()
            End If
        Catch ex As Exception
            MessageBox.Show("An error occured in frmPatientInfo. Method: btnGlucoseLevels_Click. Error: " & ex.Message)
        End Try
    End Sub

When I try to exit frmGlucose I get the following error.

ErrorMessageClose.JPG

This is the code for the exit
VB.NET:
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Try
            If hasChanged = False Then
                Me.Close()
                Exit Sub
            End If

            Dim yesNo As MsgBoxResult = MsgBox("Your changes have not been saved, " & _
                "are you sure you want to exit?", MsgBoxStyle.YesNo, "Confirm Exit")
            If yesNo = MsgBoxResult.Yes Then
                Me.Close()
                Exit Sub
            End If
            Me.Close()
        Catch ex As Exception
            MessageBox.Show("An error occured in frmGlucose. Method: btnExit_Click. Error: " & ex.Message)
        End Try
    End Sub

I have used this code in other apps and it works fine, but not here. Any suggestions.

Thanks
CoachBarker
 
Yes I did, smart enough to read the documentation, and View Detail, but still can not figure out where to apply the "Use the "new " keyword to create an object instance."

Isn't the form I am trying to close an instance of the object already? Why would I need to create a new instance if I am trying to close it?

Thanks
CoachBarker
 
The null reference can't possibly be on that line because the only reference is Me, and Me can never be a Nothing. It has to be deeper in the call stack, which is why you need to view the detail and look at the call stack. You might also need to look at the call stack for the InnerException if there is one, but I'd guess there isn't in this case.
 
Just off the top of my head, it's possible that the Me.Close gets called twice (actually there's at least 3 logic paths where Me.Close gets called)

You might want to try removing the Me.Close from the Finally block.
 
Actually the Problem was in the Designer. When I initialized the component I made this mistake

VB.NET:
  'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        ' notify the base we are closing

        CallingFrmMainScreen.Glucose = Nothing

        MyBase.Dispose(disposing)
    End Sub

instead of

VB.NET:
  'Form overrides dispose to clean up the component list.
    <System.Diagnostics.DebuggerNonUserCode()> _
    Protected Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing AndAlso components IsNot Nothing Then
            components.Dispose()
        End If
        ' notify the base we are closing

        frmMainScreen.Glucose = Nothing

        MyBase.Dispose(disposing)
    End Sub

I bet I looked at that line of code 100 times before I realized what I had done. :eek:

Thanks for the advice and sorry for the confusion
CoachBarker
 
Back
Top