我正和我的朋友一起做作业。我请求如何进行循环的帮助,他给了我那部分的代码。所以我复制并粘贴到vb中。它适用于他,但每次我尝试调试它时,我都会收到“Null exception is unhandled”的标志。但它不仅仅是一条线。拳头从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
答案 0 :(得分:0)
你没有把所有东西都搞好,它应该是这样的:
LstInvoice.Items.Clear()
注意资本I和C.
答案 1 :(得分:0)
我用一个项目测试了完全相同的代码。
正如Cody Gray已经提到的,您可能添加了错误的控件作为lstInvoice。使用ListBox。
您所做的是在粘贴代码后选择代码修复程序,并为lstInvoice生成代码存根,它会在代码底部创建以下方法:
Private Function LstInvoice() As Object
Throw New NotImplementedException
End Function
如果您忘记了命名空间引用,那些更正会提供一些有效的修复,但通常无法猜测代码的含义是什么。
因此,请删除此功能,转到表单(我认为它不是控制台应用程序),打开工具箱,然后拖放 ListBox 到您的表单。最后将ListBox名称从ListBox1重命名为 lstInvoice ,并检查您的代码是否正常运行。