变量'std'在被赋值之前使用。在运行时可能会导致空引用异常

时间:2013-07-29 10:52:01

标签: asp.net .net vb.net error-handling

为什么我会遇到这种错误。我一直在练习教程,但我仍然得到这个。应该如何清除这个错误。我已经开始练习.net了。所以请指导我

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim std As DataSet1TableAdapters.studentTableAdapter

    If txtname.Text = "" Then
        lblMessage.Text = "Please enter name"
    ElseIf txtclass.Text = "" Then
        lblMessage.Text = "Please enter class "
    ElseIf txtsemseter.Text = "" Then
        lblMessage.Text = "Please enter semester"
    Else
        std.Insert(Name:=txtname.Text, Class:=txtclass.Text, Semester:=txtsemseter.Text)
    End If
End Sub

教程链接:http://www.youtube.com/watch?v=wDFo-nGdUQY

2 个答案:

答案 0 :(得分:2)

执行以下操作

Dim std As New DataSet1TableAdapters.studentTableAdapter()

当您致电std.Insert时,您需要拥有DataSet1TableAdapters.studentTableAdapter

的对象

答案 1 :(得分:0)

您需要新建变量,如:

Dim std As New DataSet1TableAdapters.studentTableAdapter()

编辑:我试图在手机上这样做,不容易:)