vb.net “你调用的对象是空的?”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15735570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
"Object reference not set to an instance of an object?"
提问by user2221877
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
回答by Jeremy
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.
您需要创建要显示的表单的新实例。为此,您可以创建一个 T 形式的变量,然后显示它。
If you don't create an instance of your form the MainForm.Show()
code will be invoking Show()
on an null reference.
如果您不创建表单的实例,则MainForm.Show()
代码将调用Show()
空引用。
If PasswordTextBox.Text = pass Then
Me.Hide()
Dim theFormIWantToShow As New MainForm
theFormIWantToShow.Show()
End If