VB 将项目添加到不同表单的列表框中,出现空引用异常

VB Adding an item to listbox on a different form giving null reference exception

我在尝试以不同的形式将项目添加到列表框时收到 Null 引用异常。

这是我在 运行 时的错误。

An unhandled exception of type 'System.NullReferenceException' occurred in ... Additional information: Object reference not set to an instance of an object.

我试图通过在 secondForm 的 class 顶部初始化来连接 Mainform。获得数据后,我想将其添加到主窗体的列表框中。

Public Class FormHairdresser //The second form
     Dim varMainForm As FormMain //connecting the forms ?

Private Sub btnAddHairdresser_Click(sender As Object, e As EventArgs) Handles btnAddHairdresser.Click

hairdresser = HairdresserChoices(HairdresserID) // get the data
  varMainForm.lstListBox.Items.Add(hairdresser) //Run time error breaks here.

问题在于您对 formmain.with 的初始化正确初始化您正在创建的对象只不过是 Null。为了避免这种情况,我们使用 New Operator.The New 运算符通常可用于在声明实例时创建实例。 所以初始化看起来像

 Dim varMainForm As New FormMain 

希望这篇Helps.For多多参考Object Initialization Errors

更新:

 Dim varMainForm As FormMain //connecting the forms ?

Private Sub btnAddHairdresser_Click(sender As Object, e As EventArgs) Handles                      btnAddHairdresser.Click

hairdresser = HairdresserChoices(HairdresserID) // get the data
  varMainForm = New FormMain 
  varMainForm.lstListBox.Items.Add(hairdresser) //Run time error breaks here.

试试这个。

Public Class FormHairdresser //The second form

        Private Sub btnAddHairdresser_Click(sender As Object, e As EventArgs) Handles btnAddHairdresser.Click

        Dim varMainForm As FormMain 
        hairdresser = HairdresserChoices(HairdresserID) 
        varMainForm.lstListBox.Items.Add(hairdresser) 

您不能简单地创建主窗体的新实例(正如所建议的那样并希望它起作用,您需要对您创建的主窗体的实际引用。为了帮助您了解所涉及的逻辑;

创建一个新的 Winforms 项目。在默认的 Form1 中添加一个文本框和一个按钮。 现在向此应用程序添加一个新表单(您可以保留其默认名称 Form2。向此表单添加一个文本框(称为 myTextBox)和一个按钮。

现在回到您的第一个表单并双击按钮以访问代码中的点击处理程序。添加以下内容:

Dim frm as New Form2
frm.Show

按 f5 并单击按钮,您将看到一个新的表单 2。到目前为止一切顺利。

现在打开 Form2 的代码并添加以下代码,使其最终看起来像这样:

    Public Class Form2

    Private frm As Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        frm.TextBox1.Text = myTextBox.text
    End Sub

End Class

构建,按f5并点击form1上的按钮,在新form2的文本框中输入一些文本并点击按钮,你得到你的空引用异常。你得到这个的原因是因为目前私有字段 frm inForm2 指的是 Nothing.

现在打开 Form2 中的代码并添加一个构造函数和以下代码,使其最终看起来像这样:

  Public Class Form2



      Private frm As Form1
        Public Sub New(byval frm1 As Form1)
            'first we should make sure that we have a parameter to play with
            If Not IsNothing(frm1) Then
                frm = DirectCast(frm1,Form1)
            End If
        End Sub



        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            frm.TextBox1.Text = myTextBox.text
        End Sub

    End Class

最后回到您的第一个表单 buttonclick 处理程序并稍微更改代码,使其看起来像这样;

   Dim frm as New Form2(Me)

frm.Show

构建并 运行 您的应用程序,现在当您在 form2 的文本框中输入文本并单击按钮时,它会出现在 Form1 的文本框中。

之所以会出现这种情况,是因为您将应用程序启动时最初创建的 form1 的实际引用传递给了 form2。通过将该引用强制转换为用于在 form2 中表示 form1 的私有字段,您可以使用它来正确引用 form1 上的内容。这是一个非常简单的概念,但是在你进行编程之前你需要学习它。

我所要做的就是写表单名称而不是 ininalised 变量。

FormMain.lstListbox.Items.Add("item")

而不是

Dim varMainForm As FormMain varMainForm.ListBox.Items.Add("item")