Visual Basic:NullReferenceException 未处理

发布于 2024-12-14 10:36:01 字数 2301 浏览 1 评论 0 原文

我无法弄清楚是什么原因造成的。错误报告显示“未设置对象变量或 With 块变量。”对于“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

I can't work out what's causing it. The error report says "Object variable or With block variable not set." for the line "allSlotLabels(i).Image = imgCherries". This line is no different from the other, so I guess it's just the error it picked up first after randomly generating the number. Any help at all would be appreciated, I'm completely stuck.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只想待在家 2024-12-21 10:36:01

怎么样:对于 i = 0 到 2。索引从 0 开始,而不是从 1 开始。

How about: For i = 0 To 2. Indexes start with 0, not with 1.

无所谓啦 2024-12-21 10:36:01

如下更改插槽的分配:

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    allSlotLabels(0) = lblslot1
    allSlotLabels(1) = lblslot2
    allSlotLabels(2) = lblslot3
End Sub

并将循环更改为

 For i = 0 To 2

change the assignment of slots as follows:

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    allSlotLabels(0) = lblslot1
    allSlotLabels(1) = lblslot2
    allSlotLabels(2) = lblslot3
End Sub

and the loop to

 For i = 0 To 2
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文