2013-04-08 129 views
0

我正在與我的朋友分配任務。我問如何做循環的幫助,他給了我那部分的代碼。所以我複製並粘貼到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 
+1

我假定'LstInvoice'應該是顯示發票的列表框控件。你有這樣一個控制與該名稱添加到您的表單?否則,它將爲空,因爲不存在這樣的對象。 – 2013-04-08 22:28:07

+0

有這個在bottome但它增加了罰球線,它使調試系統就我擺脫 專用功能LstInvoice()的作爲對象 端功能 末級 – 2013-04-08 22:30:30

+0

這是怎麼回事是您要刪除代碼你需要。不要這樣做。如果你得到一個空引用異常,你必須調試代碼;閱讀[this](http://stackoverflow.com/questions/4660142/)。 – 2013-04-08 22:35:57

回答

0

您還沒有大寫一切正常,就應該是這樣的:

LstInvoice.Items.Clear() 

通知資本I和C.

+0

Capitalize不會改變問題,但請不要在VB.Net中添加分號。這些是真正的錯誤 – Amegon 2013-04-08 23:10:45

+0

@Amegon,是的,你可以通過我的編輯看到你的評論之前我已經刪除了分號。我的日常語言是C#,但偶爾我必須在工作時間內檢測出我的VB.NET手套。但是你必須在開玩笑說大寫字母無關緊要! – 2013-04-08 23:13:58

+0

我的意思是說你是對的,大寫字母不是100%完美的,但這與任何解決方案都沒有關係。在VB.Net中,情況並不重要!實際上,它甚至可以作爲一個額外的控制,通過寫正確大小寫的deklarations,但所有其他行只能使用小寫字母。如果對象被正確識別,則名稱將被正確自動大寫。 – Amegon 2013-04-08 23:35:16

0

我測試了完全相同的代碼與一個項目。

正如Cody Gray所提到的,您可能已將錯誤控件添加爲lstInvoice。使用列表框。

做過什麼,是選擇一個代碼修復您粘貼代碼後,並生成代碼存根lstInvoice,它在你的代碼的底部產生下面的方法:

Private Function LstInvoice() As Object 
    Throw New NotImplementedException 
End Function 

這些更正提供了一些工作修復瞭如果您忘記了命名空間引用,但通常無法猜測代碼的意義。

因此,請刪除此功能,轉到窗體(我想它不是控制檯應用程序),打開工具箱,然後拖放一個列表框到您的窗體。最後,將列表框名稱從ListBox1重命名爲lstInvoice並檢查您的代碼是否正在運行。