Vb.net 对象引用未设置为对象的实例。不知道为什么我';我得到了这个错误

Vb.net 对象引用未设置为对象的实例。不知道为什么我';我得到了这个错误,vb.net,Vb.net,我正在编写的程序遇到一些问题,每次我尝试运行运行此代码的窗体时: Private Sub customDuffer_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles customDuffer.DoWork While (Me.Visible = True) For Each tone In trackWriter.noteArray

我正在编写的程序遇到一些问题,每次我尝试运行运行此代码的窗体时:

Private Sub customDuffer_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles customDuffer.DoWork
    While (Me.Visible = True)
        For Each tone In trackWriter.noteArray
            If switch = True Then
                Beep.tone(1000, note, 240)
            End If
        Next tone
    End While

Beep.tone(1000,note,240)Beep或note变量必须未初始化。添加一些调试代码:

If switch = True Then
  If Beep is Nothing then MsgBox ("Beep is nothing!")
  If note is Nothing then MsgBox ("note is nothing!")
  Beep.tone(1000, note, 240)
End If
运行它,查看弹出的消息。然后确定应该在哪里进行初始化


如果两条消息均未弹出,则Beep.tone()方法中存在未初始化的变量错误。

Notes参数如何?它有什么数据类型?Beep是一个类,我所做的只是引用它来生成一个Beep音调,通过扬声器播放。当我去掉抛出异常的行并将其更改为“if tone is nothing the msgBox(“tone is nothing.”)。当我这样做时,异常移动到带有for each循环的行。相同的错误,不同的行。我猜trackWriter或noteArray没有初始化?这是你的代码,还是你正在编辑的其他人的代码?哼。它不应该用于trackwriter.notearray中的每个音符,是吗?(这会更有意义,因为note将是Beep.Tone调用的一个参数)这是我的代码,Beep类在其他任何地方都可以正常工作。我有另一个对象以相同的形式运行beep类。还有trackWriter,我可以在项目的任何地方访问。它是用一个名为“trackWriter”的表单初始化的,我可以在后台工作人员之外用完全相同的代码从主菜单表单访问和使用它。我现在开始工作,我必须在出于某种原因调用变量之前重新初始化变量,所以我只是将trackWriter.noteArray复制到另一个名为“secondSounds”的变量中而是引用了这个。现在一切都好了:D