对象引用未设置为对象的实例Null引用异常未处理

时间:2014-05-08 04:11:52

标签: vb.net visual-studio vb.net-2010

当我尝试从检查用户名是否已在数据库中使用的函数创建新帐户时,我一直收到错误。这是我的代码。

Public Class CreatePatientAccountPage1

    Private Sub Button8_Click(sender As System.Object, e As System.EventArgs)
        MainScreen.Show()
        Me.Hide()
    End Sub

    Private Sub Button7_Click_1(sender As System.Object, e As System.EventArgs) Handles Button7.Click
        If CreatePatientaccountTableAdapter1.CheckUsername(UsernameTextBox2.Text) = Nothing Then
            CreatePatientaccountTableAdapter1.CreateAccount(UsernameTextBox2.Text, PasswordTextBox2.Text)
            MsgBox("Account Created")
        Else
            MsgBox("Username is taken")
        End If

        CreatePatientAccountPage2.Show()
        Return
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

在这里,我可以看到你没有为你调用的那些类创建任何对象。

使用以下行而不是代码

 dim obj1 as new CreatePatientaccountTableAdapter1
 dim obj2 as new CreatePatientAccountPage2
 If obj1.CheckUsername(UsernameTextBox2.Text) = Nothing Then
        obj2.CreateAccount(UsernameTextBox2.Text, PasswordTextBox2.Text)
        MsgBox("Account Created")
    Else
        MsgBox("Username is taken")
    End If

    obj2.Show()