家伙!我正在为我的最终项目创建一个自动售货机应用程序,但遇到了一个小问题。当我尝试运行它时,我收到以下错误,创建表单时出错。有关详细信息,请参阅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
结束班
答案 0 :(得分:0)
在加载控件之前,不要尝试从控件获取值 - 根据类范围中的控件属性声明变量。只需声明变量并找出需要设置的位置和时间 - 否则它仍为空 - 对吧?
编辑:Hans Passant
说的是什么。