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