"对象引用未设置为对象的实例?"

时间:2013-03-31 23:03:17

标签: vb.net

如果文本框的文本正确,请尝试显示其他表单。当我调试时,我得到一个错误,说“对象引用未设置为对象的实例”。代码如下:

'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

1 个答案:

答案 0 :(得分:1)

您需要创建要显示的表单的新实例。您可以通过创建表单T的变量然后显示它来完成此操作。

如果您没有创建表单实例,MainForm.Show()代码将在空引用上调用Show()

If PasswordTextBox.Text = pass Then
    Me.Hide()
    Dim theFormIWantToShow As New MainForm
    theFormIWantToShow.Show()
End If