Vb.net 从其他表单Visual Basic设置PictureBox图像时,对象引用未设置为对象的实例

Vb.net 从其他表单Visual Basic设置PictureBox图像时,对象引用未设置为对象的实例,vb.net,object,Vb.net,Object,我正在使用以下代码从themes.vb在Tab.vb中设置图片: Public objForm As Object 'This is at the top of the page, under Public Class Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles RadioButton1.CheckedChanged If RadioButton1.Ch

我正在使用以下代码从themes.vb在Tab.vb中设置图片:

 Public objForm As Object 'This is at the top of the page, under Public Class

 Private Sub RadioButton1_CheckedChanged(sender As Object, e As EventArgs) Handles     RadioButton1.CheckedChanged 
    If RadioButton1.Checked = True Then
        CType(objForm, tab).PictureBox1.Image = Image.FromFile("Abstract.png")


    End If
End Sub
但是,当检查单选按钮时,我收到以下消息:

Redient Browser.exe中发生类型为“System.NullReferenceException”的未处理异常

其他信息:对象引用未设置为对象的实例

救命啊

有了这个:

CType(objForm, tab).PictureBox1.Image
很可能无法进行转换(从表单到选项卡?),因此结果不会导致NRE

标题表示PictureBox位于另一个窗体上。与其摆弄另一个表单上的控件,不如简单地向它提供新数据,让它完成工作:

Public Class FormWithPictureBox

    Private currentFile As String
    Public Sub SetNewImage(filename As String)

       If currentFile <> filename Then
          ' TooDo: check if file exists, clean up
          Me.PictureBox.Image = Image.FromFile(filename)
          currentFile = filename
       End If
    ...

这使得表单能够处理自己的控件,并为其他操作提供了一个单一的入口点,以指示表单执行某些操作。如果/当您试图找出图像更改的原因时,您可以在
SetNewImage
中设置断点以查看调用方是谁。

什么是
objForm
选项卡
?看起来您正试图将表单强制转换为一个不会很好结束的选项卡。如果演员阵容失败,你将一无所获,并获得NRE。把它切掉,然后
PictureBox1.Image=…
应该可以正常工作,只要它在这个表单上,就可以复制
Public Class OtherForm
    Private Sub RadioButton1_CheckedChanged(...
        If RadioButton1.Checked = True Then
            objForm.SetNewImage(theFileName)
        End If
    End Sub