1

我试图弄清楚为什么我的代码会引发空引用异常。

我正在尝试将对象添加到列表中。该对象可以是 4 种类型之一,我在相关代码之后拥有所有定义。此代码位于 select case 语句中 4 个图片框的按钮单击处理程序中

下面是有问题的代码

    Dim count As Integer = 0
    Dim a_component As Object = Nothing

    Select Case (p.Name)
        Case TranspositorPictureBox.Name
            Form2.ShowDialog(Me)
            count = TNumericUpDown.Value
            a_component = New Transpositor(tempTranspositorDivert)
        Case ZonePictureBox.Name
            count = ZNumericUpDown.Value
            a_component = New Zone()
        Case InductionPictureBox.Name
            count = IndNumericUpDown.Value
            a_component = New Induction()
        Case InclinePictureBox.Name
            count = IncNumericUpDown.Value
            a_component = New Incline()
    End Select

    For i = 1 To count
        Dim newPic As PictureBox = New PictureBox()
        newPic.Image = p.Image
        newPic.Size = p.Size
        newPic.SizeMode = p.SizeMode

        sys.Add(a_component)

        LayoutFlowLayout.Controls.Add(newPic)
    Next

这是类定义。变量 sys 的类型是 TranSorter

Public Class TranSorter
Public width As Integer
Public components As List(Of Object)

Public Sub New(ByVal the_width As Integer)
    Me.width = the_width
    Me.components = New List(Of Object)
End Sub

Public Sub Add(ByVal next_component As Object)
    Me.components.Add(next_component)
End Sub

End Class

Public Class Transpositor
Public length As Integer
Public divert As Object

Public Sub New(ByVal a_divert As Object)
    Me.divert = a_divert
    Me.length = ComponentLengths.TranspositorLength
    Form1.Transpositors += 1
End Sub
End Class

Public Class Zone
Public length As Integer

Public Sub New()
    Me.length = ComponentLengths.ZoneLength
    Form1.Microzones += 1
End Sub
End Class

Public Class Induction
Public length As Integer

Public Sub New()
    Me.length = ComponentLengths.InductionLength
    Form1.Inductions += 1
End Sub
End Class

Public Class Incline
Public length As Integer

Public Sub New()
    Me.length = ComponentLengths.InclineLength
    Form1.Inclines += 1
End Sub
End Class

sys.add 行正在引发异常。这是我初始化 sys 的代码

Dim sys As TranSorter

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)

sys = New TranSorter(temp_width)
4

2 回答 2

1

我怀疑出于某种原因,您的 p.Name 不等于您在 If 子句中检查的四种情况之一。我建议考虑添加一揽子 Else 子句和/或检查 a_component 是否为空,然后再将其添加为额外的预防措施。

Select Case (p.Name)
    Case TranspositorPictureBox.Name
        Form2.ShowDialog(Me)
        count = TNumericUpDown.Value
        a_component = New Transpositor(tempTranspositorDivert)
    Case ZonePictureBox.Name
        count = ZNumericUpDown.Value
        a_component = New Zone()
    Case InductionPictureBox.Name
        count = IndNumericUpDown.Value
        a_component = New Induction()
    Case InclinePictureBox.Name
        count = IncNumericUpDown.Value
        a_component = New Incline()
    Case Else
        Exit Sub ' Function/etc
End Select

if a_component IsNot Nothing Then
  For i = 1 To count
    Dim newPic As PictureBox = New PictureBox()
    newPic.Image = p.Image
    newPic.Size = p.Size
    newPic.SizeMode = p.SizeMode

    sys.Add(a_component)

    LayoutFlowLayout.Controls.Add(newPic)
  Next
End If
于 2012-10-04T14:14:54.437 回答
1

除非 sys 是 Nothing,否则您的代码不应引发任何异常。
这可能是您在哪里初始化 sys var 的 Form_Load 事件没有执行。只需在行上设置断点即可轻松检查这种情况

 sys = New TranSorter(temp_width) 

然后在

 sys.Add(a_component) 

行并检查 sys var 是否为空

于 2012-10-04T14:27:49.170 回答