-1

The first 3 times i ran this code, it worked as I intended. Now whenever I run it, it closes and in the debug screen, it prints: "A first chance exception of type 'System.NullReferenceException' occurred in Battleship - 1 Player.exe". Sorry for the long piece of code, I "collapsed" the properties since they are pretty much the same in every one.

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
alexanderd5398
  • 329
  • 1
  • 4
  • 13

1 Answers1

1

There are several issues with this:

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 is used for loop control in several places, and assuming it is correct. since it is a public property something else anywhere in the app could be changing it. Perhaps you pass it and use it in the constructor as you do to create those arrays, but you really do not need to save it. And you really do not need to make it a public property.

ALL the loops in those procedures (some which themselves should be private) should generally (re) calculate the amount to loop and use a local variable, even if length is private because something else in the class might change it inadvertently.

Private length As Integer

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

If it does something more than we can see, it still need not be a property, a private variable is enough.

Ňɏssa Pøngjǣrdenlarp
  • 37,255
  • 11
  • 50
  • 147