Visual Basic: исключение NullReferenceException было необработано

Я не могу понять, что ее вызывает. В отчете об ошибке написано: «Переменная объекта или переменная блока не задана». для строки «allSlotLabels (i) .Image = imgCherries». Эта строка ничем не отличается от другой, поэтому я предполагаю, что это просто ошибка, которую она обнаружила первой после случайного генерирования числа. Любая помощь будет оценена по достоинству, я полностью застрял.

Public Class frmSlotMachine

' Declare all variables needed
Dim startingCoins As Integer = 5
Dim coins As Integer = startingCoins + 1
Dim numbersGenerated As Integer = 20
Dim spinStatus As String = "Start"
Dim held1 As Boolean = False
Dim held2 As Boolean = False
Dim held3 As Boolean = False
Dim slot1Name, slot2Name, slot3Name As String
Dim slot1Value, slot2Value, slot3Value As Integer
' Assign resources to variables
Dim imgBanana As Image = My.Resources.banana
Dim imgOrange As Image = My.Resources.orange
Dim imgSeven As Image = My.Resources.seven
Dim imgCherries As Image = My.Resources.cherries
Dim imgBatman As Image = My.Resources.batman
Dim imgCross As Image = My.Resources.cross
' Declare arrays
Dim allHelds() As Boolean = {held1, held2, held3}
Dim allSlotValues() As Integer = {slot1Value, slot2Value, slot3Value}
Dim allSlotNames() As String = {slot1Name, slot2Name, slot3Name}
Dim allSlotLabels() As Object = {lblSlot1, lblSlot2, lblSlot3}

Private Sub btnSpin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSpin.Click


    ' Trying a for loop to randomise numbers and assign images, if hold is off
    For i = 1 To 3
        If Not allHelds(i) Then
            allSlotValues(i) = Int(Rnd() * numbersGenerated + 0.5)
            Select Case allSlotValues(i)
                Case 0 To 5
                    allSlotLabels(i).Image = imgBanana
                    allSlotNames(i) = "Banana"
                Case 6 To 11
                    allSlotLabels(i).Image = imgOrange
                    allSlotNames(i) = "Orange"
                Case 12 To 16
                    allSlotLabels(i).Image = imgCherries
                    allSlotNames(i) = "Cherries"
                Case 17 To 19
                    allSlotLabels(i).Image = imgSeven
                    allSlotNames(i) = "Seven"
                Case 20
                    allSlotLabels(i).Image = imgBatman
                    allSlotNames(i) = "Batman"
                Case Else
                    allSlotLabels(i).Text = "Error. Current slot value = " & allSlotValues(i)
            End Select
        End If
    Next
0
задан GSerg 9 November 2011 в 19:55
поделиться