-1

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
Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64
Fiery-Skyline
  • 67
  • 1
  • 4

2 Answers2

0

This will show your user that they went over and skip the adding of the roll to the total to leave them in place.

Change:

'input
intValue = intRoll
intScore = intScore + intValue
strCounterName = "lblcounter" & intScore.ToString

To:

'input
intValue = intRoll
If intScore + intValue > 30 Then
    MsgBox "OvErBoArD"
Else
    intScore = intScore + intValue
End If
strCounterName = "lblcounter" & intScore.ToString
Chrismas007
  • 6,085
  • 4
  • 24
  • 47
-2

Since there are only 30 counters when intScore goes over 30 it will throw this exception.

Add an If statement to your input as follows:

'input
intValue = intRoll
If intscore + intvalue < 30 then 
intscore=intscore + intvalue
End if

strCounterName = "lblcounter" & intScore.ToString

This way it will not go over thirty before it sets the lblcounter and will not bug out

Dman
  • 553
  • 1
  • 11
  • 33
  • This doesn't fulfill the user's criteria of "I want the Marker to stay where it is if the number rolled would bring it past 30." – Chrismas007 Dec 24 '14 at 18:27