夥計!我爲我的最終項目創建了自動售貨機應用程序,但遇到了一個小問題。當我嘗試運行它時,出現以下錯誤,創建窗體時發生錯誤。有關詳細信息,請參閱Exception.InnerException。錯誤是:對象引用未設置爲對象的實例。我假設它與我的label.text有關,因爲這是問題出現時我已轉換爲double的。如果有人能夠爲我提供解決方法,將非常感謝!VB錯誤創建表格
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
末級
「balanceLabel」爲Nothing,您需要一個構造函數(Sub New),並且不會在* InitializeComponent()之後使用該標識符。 –