“未将对象引用设置为对象的实例?" [英] "Object reference not set to an instance of an object?"

查看:37
本文介绍了“未将对象引用设置为对象的实例?"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果文本框的文本正确,则尝试显示其他形式.当我调试时,我收到一条错误消息,提示对象引用未设置为对象的实例".代码如下:

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

推荐答案

您需要创建要显示的表单的新实例.为此,您可以创建一个 T 形式的变量,然后将其显示出来.

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.

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

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

这篇关于“未将对象引用设置为对象的实例?"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆