1

Trying to show other form if textbox's text is correct. When I debug I get an error saying "Object reference not set to an instance of an object". The code is below:

'OK is OK button, MainForm is the form I'm trying to open     

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
    Dim pass As String = My.Computer.FileSystem.ReadAllText("password.txt")
    If PasswordTextBox.Text = pass Then
        MainForm.Owner = Me
        Me.Hide()
        '"Object reference not set to an instance of an object" error when debugging on line below
        MainForm.Show()
    End If
End Sub
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user2221877
  • 35
  • 1
  • 3
  • 6

1 Answers1

1

You need to create a new instance of the form you want to display. You do this by creating a variable of form T and then showing it.

If you don't create an instance of your form the MainForm.Show() code will be invoking Show() on an null reference.

If PasswordTextBox.Text = pass Then
    Me.Hide()
    Dim theFormIWantToShow As New MainForm
    theFormIWantToShow.Show()
End If
Jeremy
  • 3,880
  • 3
  • 35
  • 42