我试图弄清楚为什么我的代码会引发空引用异常。
我正在尝试将对象添加到列表中。该对象可以是 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)