0

伙计们!我正在为我的最终项目创建一个自动售货机应用程序,但遇到了一个小问题。当我尝试运行它时,我收到以下错误,创建表单时发生错误。有关详细信息,请参阅 Exception.InnerException。错误是:对象引用未设置为对象的实例。我假设它与我已转换为双精度的 label.text 有关,因为那是问题出现的时候。如果有人可以为我提供解决方法,将不胜感激!

Public Class VendingMachine
Dim balance As Double = CDbl(balanceLabel.Text)
'Dim intbalance As String = balanceLabel.Text
'Dim balance As Double = Convert.ToDouble(intbalance)


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles depositButton.Click
    If nickleButton.Checked = True Then
        balance = +0.05
        balanceLabel.Text = String.Format("{0:C}", balance)
    End If

    If dimeButton.Checked = True Then
        balance = +0.1
        balanceLabel.Text = String.Format("{0:C}", balance)
    End If

    If quarterButton.Checked = True Then
        balance = +0.25
        balanceLabel.Text = String.Format("{0:C}", balance)
    End If

    If dollarButton.Checked = True Then
        balance = +1.0
        balanceLabel.Text = String.Format("{0:C}", balance)
    End If
End Sub


Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles refundButton.Click
    balance = 0.0
    balanceLabel.Text = String.Format("{0:C}", balance)
    MsgBox("Money Refunded")
End Sub

Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles starburstPicture.Click
    If balance >= 1.25 Then
        balance = balance - 1.25
    Else
        MsgBox("You do not have enough money to purchace that item.")
    End If
End Sub

Private Sub jollyrancherPicture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles jollyrancherPicture.Click
    If balance >= 1.0 Then
        balance = balance - 1.0
    Else
        MsgBox("You do not have enough money to purchace that item.")
    End If
End Sub

Private Sub gummyPicture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles gummyPicture.Click
    If balance >= 0.75 Then
        balance = balance - 0.75
    Else
        MsgBox("You do not have enough money to purchace that item.")
    End If
End Sub

Private Sub peppermintPicture_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles peppermintPicture.Click
    If balance >= 0.75 Then
        balance = balance - 0.75
    Else
        MsgBox("You do not have enough money to purchace that item.")
    End If
End Sub

Private Sub VendingMachine_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

结束类

4

1 回答 1

0

在加载之前不要尝试从控件中获取值 - 根据类范围内的控件属性声明变量。只需声明变量并确定需要设置的位置和时间 - 否则无论如何它都是空的 - 对吗?

编辑:怎么Hans Passant说。

于 2013-05-14T21:37:08.447 回答