null引用异常未处理

时间:2014-12-12 11:01:27

标签: vb.net

Public Class Form3
    Inherits Windows.Forms.Form
    Public F1 As Form1
    Dim S1 As String = F1.TextBox2.Text


    Public Sub New()
        InitializeComponent()
        BindGrid()
    End Sub
    Private Sub bindgrid()


        Dim con As New SqlConnection
        '/ Dim cmd As New SqlCommand
        con.ConnectionString = "server=hcl-pc\SQLEXPRESS;Initial Catalog=UBGB_HRMS;Integrated Security=True"


        ''/Dim S2 As String

        '/ S1 = F1.TextBox1.Text
        '/ S2 = F1.TextBox2.Text

        ''/If String.IsNullOrEmpty(S1) Then
        '/Return
        '/ End If
        Using cmd As New SqlCommand("SELECT *FROM emp_personal_details where emp_adhaar=s1", con)
            cmd.CommandType = CommandType.Text
            Using sda As New SqlDataAdapter(cmd)
                Using dt As New DataTable()
                    sda.Fill(dt)
                    DataGridView1.DataSource = dt
                End Using
            End Using
        End Using

    End Sub
    Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick

    End Sub
End Class

我使用多个窗体并在form1中分配form3的值。变量Form1需要s1。在调试时,我收到错误:

  

null引用未被隐藏

1 个答案:

答案 0 :(得分:2)

我认为问题在于您为变量F1分配了一个类型,但没有实例化它。尝试将声明更改为:

Public F1 As New Form1 

但是,这会创建一个新的空白form1,它在textbox(可能)中没有任何内容,我假设它不是您想要做的。你真的想在其他地方声明变量F1,以便你可以从form3引用它的当前实例。