2013-05-14 84 views
0

夥計!我爲我的最終項目創建了自動售貨機應用程序,但遇到了一個小問題。當我嘗試運行它時,出現以下錯誤,創建窗體時發生錯誤。有關詳細信息,請參閱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 

末級

+1

「balanceLabel」爲Nothing,您需要一個構造函數(Sub New),並且不會在* InitializeComponent()之後使用該標識符。 –

回答

0

它的加載之前,不要試圖從控制獲取值 - 宣佈基於類範圍的控制特性的變量。只需聲明變量並確定需要設置的位置和時間,否則它無論如何都是空的 - 對嗎?

編輯:什麼Hans Passant說。

+0

是的,這是有道理的哈哈。謝謝你們倆!! –

+1

「Hans Passant說的」不是答案。 –

+0

創建表單時出現錯誤,Hans和我在這件事上都是正確的 - 他只需要更好地解釋它。謝謝你的無益評論肯。 – OneFineDay