这个代码在我运行它的前几次正常工作,现在抛出异常:“System.NullReferenceException”

时间:2014-05-15 21:27:25

标签: vb.net random nullreferenceexception

我运行此代码的前3次,它按照我的意图工作。现在每当我运行它,它关闭并在调试屏幕中,它打印:"类型' System.NullReferenceException'的第一次机会异常。发生在战舰 - 1 Player.exe"。很抱歉,这段很长的代码,我已经崩溃了#34;属性,因为它们在每个属性中几乎相同。

Public Class Enemy
    Dim _name As String
    Public Property name() As String ...

    Dim _length As Integer
    Public Property length As Integer ...

    Dim _start_point() As Integer
    Public Property start_point() As Integer() ...

    Dim _space_filled(,) As Integer
    Public Property space_filled As Integer(,) ...

    Dim _direction As String
    Public Property direction() As String ...

    Shared gen As New Random()
    Public x As Integer = gen.Next(0, 10)
    Public y As Integer = gen.Next(0, 10)

    Public Sub New(ByVal namep As String, ByVal lengthp As Integer)
        name = namep
        length = lengthp

        ReDim _start_point(2)
        ReDim _space_filled(length, 2)

        GenerateStartPoint()
        GenerateDirection()
        ExtendStartPoint()

        DefineFilled()
        ColorFilled()
    End Sub

    Public Sub GenerateStartPoint()
        start_point = {x, y}
    End Sub

    Public Sub GenerateDirection()
        If gen.Next(0, 2) = 0 Then
            direction = "horizontal"
        Else
            direction = "vertical"
        End If
    End Sub

    Public Sub ExtendStartPoint()
        If direction = "horizontal" Then
            For i As Integer = 0 To length - 1
                space_filled(i, 0) = start_point(0) + i
                space_filled(i, 1) = start_point(1)
            Next
        ElseIf direction = "vertical" Then
            For i As Integer = 0 To length - 1
                space_filled(i, 0) = start_point(0)
                space_filled(i, 1) = start_point(1) + i
            Next
        End If
    End Sub

    Public Sub DefineFilled()
        For i As Integer = 0 To length - 1
            x = space_filled(i, 0)
            y = space_filled(i, 1)
            Main.TrackerBoard.box_list(x, y).full = True 'Error is coming from here.
        Next
    End Sub

    Private Sub ColorFilled()
        For y As Integer = 0 To 9
            For x As Integer = 0 To 9
                'Debug.Print(Main.PlayerBoard.box_list(x, y).full)
                If Main.TrackerBoard.box_list(x, y).full = True Then
                    Main.TrackerBoard.box_list(x, y).image.BackColor = System.Drawing.Color.Red
                Else
                    Main.TrackerBoard.box_list(x, y).image.BackColor = System.Drawing.Color.Silver
                End If
            Next
        Next
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

这有几个问题:

Public Property length As Integer
'...
Public Sub DefineFilled()
    For i As Integer = 0 To length - 1
        x = space_filled(i, 0)
        y = space_filled(i, 1)
        Main.TrackerBoard.box_list(x, y).full = True 'Error is coming from here.
    Next
End Sub

length用于几个地方的循环控制,并假设它是正确的。因为它是一个公共财产,应用程序中任何地方的其他东西都可以改变它。也许你传递它并在构造函数中使用它来创建那些数组,但你真的不需要保存它。你真的不需要把它变成公共财产。

这些过程中的所有循环(其中一些本身应该是私有的)通常应该(重新)计算循环量并使用局部变量,即使长度是私有的,因为类中的其他东西可能会无意中更改它。

Private length As Integer

' nothing outside this class needs to call this ever
Private Sub DefineFilled() ...

如果它做的比我们看到的更多,它仍然不需要是属性,私有变量就足够了。