user2893432
user2893432

Reputation: 13

Error on form click object reference not set to an instance of an object

When I click form1 button 1 to show form2 but it show The error is:

Object reference not set to an instance of an object.

Form1:

Public Class Form1
 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Form2. Show()
    Me.Hide()

 End Sub
End Class

Upvotes: 0

Views: 1325

Answers (2)

Grahamvs
Grahamvs

Reputation: 679

I suggest declaring it outside of the method, this way you can access it from other methods:

Private _Form2 as new Form2

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

 Me._Form2.Show()
 Me.Hide()

End Sub

Upvotes: 1

Makita
Makita

Reputation: 746

Try this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim newform as New Form2
    newform.Show()
    Me.Hide()
End Sub

You can have more than one of the same form open, so you need to create an 'instance' of the form, before you tell it to show that instance.

Upvotes: 0

Related Questions