我正在與我的朋友分配任務。我問如何做循環的幫助,他給了我那部分的代碼。所以我複製並粘貼到vb中。它適用於他,但每次我嘗試調試它時,我都會收到「空的異常未處理」符號。但它不只是一條線。拳頭從LstInvoice.items.clear()開始,但如果我刪除它,它會遍歷所有行。到底是怎麼回事?我之前在其他任務中使用過LstInvoice.items.clear(),之前從未遇到過這個問題。這裏是我的代碼:空異常未處理?
Private Sub btnStraight_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStraight.Click
Dim Cost As Double
Cost = txtCost.Text
Dim Salvage_Value As Double
Salvage_Value = 0
Dim Life As Double
Life = txtLife.Text
Dim Depreciation As Double
Depreciation = (Cost/Life)
Dim c As Integer, i As Integer, x As Integer, y As Integer, z As Integer
c = CInt(CDbl(txtYear.Text))
i = CInt(txtLife.Text)
x = CInt(txtCost.Text)
y = CInt(CDbl(x)/i)
z = x - y
LstInvoice.items.clear()
LstInvoice.Items.Add("Description: " & "" & txtDescription.Text)
LstInvoice.Items.Add("Year of purchase: " & txtYear.Text)
LstInvoice.Items.Add("Cost: " & FormatCurrency(txtCost.Text))
LstInvoice.Items.Add("Estimated life:" & txtLife.Text)
LstInvoice.Items.Add("Method of Depresciation: straight-line method")
LstInvoice.Items.Add("")
LstInvoice.Items.Add("Value beginning of " & c & ": " & FormatCurrency(CInt(txtCost.Text)))
LstInvoice.Items.Add("Amount of depreciation accruing: " & c & ": " & FormatCurrency(y))
LstInvoice.Items.Add("Total depreaciation at end of " & c & ": " & FormatCurrency(z))
LstInvoice.Items.Add("")
c = c + 1
Do While (x > 0)
y = CInt(CDbl(x)/i)
z = x - y
x = z
LstInvoice.Items.Add("Value beginning of " & c & ": " & FormatCurrency(x))
LstInvoice.Items.Add("Amount of depreciation accruing: " & c & ": " & FormatCurrency(y))
LstInvoice.Items.Add("Total depreaciation at end of " & c & ": " & FormatCurrency(z))
LstInvoice.Items.Add("")
i = i - 1
c = c + 1
Loop
我假定'LstInvoice'應該是顯示發票的列表框控件。你有這樣一個控制與該名稱添加到您的表單?否則,它將爲空,因爲不存在這樣的對象。 – 2013-04-08 22:28:07
有這個在bottome但它增加了罰球線,它使調試系統就我擺脫 專用功能LstInvoice()的作爲對象 端功能 末級 – 2013-04-08 22:30:30
這是怎麼回事是您要刪除代碼你需要。不要這樣做。如果你得到一個空引用異常,你必須調試代碼;閱讀[this](http://stackoverflow.com/questions/4660142/)。 – 2013-04-08 22:35:57