【问题标题】:Object reference not set to an instance of an object Null Reference Exception was unhandled对象引用未设置为对象的实例 未处理空引用异常
【发布时间】:2014-06-25 07:18:13
【问题描述】:

当我尝试通过检查用户名是否已在数据库中使用的函数创建新帐户时,我不断收到错误消息。这是我的代码。

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

【问题讨论】:

  • 异常发生在哪一行?也请使用相关的 Visual Basic 标签(例如 VB6、VB.net)标记您的问题
  • 异常发生在 If CreatePatientaccountTableAdapter1.CheckUsername(UsernameTextBox2.Text) = Nothing Then
  • 然后在该行代码上放一个断点,检查这两件事中哪一个是空的。
  • 我应该在哪里放置 Me 关键字?像这样? Me.CreatePatientaccountTableAdapter1.CheckUsername(UsernameTextBox2.Text) = Nothing Then
  • 尝试使用Is Nothing 而不是= Nothing。还要检查 CheckUserName 的返回类型。

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


【解决方案1】:

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

使用以下行代替您的代码,

 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()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-12-30
    • 1970-01-01
    • 2013-06-14
    • 1970-01-01
    • 1970-01-01
    • 2012-08-04
    • 1970-01-01
    • 2013-03-08
    相关资源
    最近更新 更多