I have to create a snakes and ladders game for a project. I have 30 spaces on my board. if the Marker goes past 30 the application crashes with :
"An unhandled exception of type 'System.NullReferenceException' occurred in appCounter.exe
Additional information: Object reference not set to an instance of an object."
I want the Marker to stay where it is if the number rolled would bring it past 30.
here is my code:
Public Class frmBoard
Dim intScore As Integer
Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.Click
'declartions
Dim intValue As Integer
Dim strCounterName As String
Dim roll As New Random()
Dim intRoll = roll.Next(1, 7)
'make the current counter visible
If intScore > 0 Then
strCounterName = "lblCounter" & intScore
Me.Controls(strCounterName).Visible = False
End If
'input
intValue = intRoll
intScore = intScore + intValue
strCounterName = "lblcounter" & intScore.ToString
'output
txtMove.Text = intRoll
Me.Controls(strCounterName).Visible = True
If intScore = 3 Then
lblCounter3.Visible = False
lblCounter22.Visible = True
intScore = 22
End If
If intScore = 5 Then
lblCounter5.Visible = False
lblCounter8.Visible = True
intScore = 8
End If
If intScore = 11 Then
lblCounter11.Visible = False
lblCounter26.Visible = True
intScore = 26
End If
If intScore = 20 Then
lblCounter20.Visible = False
lblCounter29.Visible = True
intScore = 29
End If
If intScore = 17 Then
lblCounter17.Visible = False
lblCounter4.Visible = True
intScore = 4
End If
If intScore = 19 Then
lblCounter19.Visible = False
lblCounter7.Visible = True
intScore = 7
End If
If intScore = 21 Then
lblCounter21.Visible = False
lblCounter9.Visible = True
intScore = 9
End If
If intScore = 27 Then
lblCounter27.Visible = False
lblCounter1.Visible = True
intScore = 1
End If
If intScore = 31 Then
lblCounter31.Visible = False
lblCounter1.Visible = True
intScore = 1
End If
If intScore = 32 Then
lblCounter32.Visible = False
lblCounter1.Visible = True
intScore = 1
End If
If intScore = 33 Then
lblCounter33.Visible = False
lblCounter1.Visible = True
intScore = 1
End If
If intScore = 30 Then
MsgBox("Winner")
End If
If intScore > 30 Then
MsgBox("OvErBoArD")
End If
End Sub
End Class