1

コードがnull参照例外をスローする理由を理解しようとしています。

リストにオブジェクトを追加しようとしています。オブジェクトは、問題のコードの後に​​すべての定義がある4つのタイプのいずれかになります。このコードは、selectcaseステートメントの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

sys が Nothing でない限り、コードは例外をスローしてはなりません。
これは、sys 変数を初期化する Form_Load イベントが実行されない場合に発生する可能性があります。この状況は、行にブレークポイントを設定するだけで簡単に確認できます

 sys = New TranSorter(temp_width) 

次に、別のブレークポイントを

 sys.Add(a_component) 

行して、sys varが何もないかどうかを確認します

于 2012-10-04T14:27:49.170 に答える
1

何らかの理由で、 p.Name が If 句でチェックする 4 つのケースのいずれとも一致しないと思われます。追加の予防措置として、ブランケット 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 に答える