【问题标题】:Null exception was unhandled?空异常未处理?
【发布时间】:2013-03-31 04:25:54
【问题描述】:

我正在和我的朋友一起完成一项任务。我就如何进行循环寻求帮助,他给了我那部分的代码。所以我复制并粘贴到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 应该是一个显示发票的列表框控件。您是否有这样的控件,该控件已添加到您的表单中?否则,它将为 null,因为不存在这样的对象。
  • 底部有这个,只是它添加了一条抛出线,调试系统让我摆脱了 Private Function LstInvoice() As Object End Function End Class
  • 您正在删除您需要的代码。不要那样做。如果你得到一个空引用异常,你必须调试代码;阅读this
  • 除了其他响应之外,将 Option Strict On 放在代码顶部或进入项目属性并设置 Option Strict On。并将其设为所有新项目的默认设置。它会为您省去很多麻烦。

标签: .net vb.net nullreferenceexception


【解决方案1】:

你没有正确地大写所有的东西,它应该是这样的:

LstInvoice.Items.Clear()

注意大写的 I 和 C。

【讨论】:

  • 大写不会改变问题,但请不要在VB.Net中添加分号。这些是真正的错误
  • @Amegon,是的,正如您在评论之前的编辑中看到的那样,我已经删除了分号。我的日常语言是 C#,但即使在工作中,我也时不时地必须戴上我的 VB.NET 手套。但你一定是在跟我开玩笑,大写并不重要!
  • 我的意思是你是对的,大写并不是 100% 完美的,但这与这里的任何解决方案都没有关系。在 VB.Net 中,情况无关紧要!实际上,它甚至可以通过使用正确的大写来编写 deklarations 来用作附加控件,但所有其他行仅使用小写字母。如果对象被正确识别,则名称将正确自动大写。
【解决方案2】:

我用一个项目测试了完全相同的代码。

正如 Cody Gray 已经提到的,您可能添加了错误的控件作为 lstInvoice。使用列表框。

您所做的是在粘贴代码后选择一个代码修复程序,并为 lstInvoice 生成一个代码存根,它会在您的代码底部创建以下方法:

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

如果您忘记了命名空间引用,这些更正提供了一些有效的修复,但通常它无法猜测代码是针对哪个控件的。

所以请删除这个函数,转到表单(我想它不是控制台应用程序),打开工具箱,然后将 ListBox 拖放到您的表单中。最后将该 ListBox 名称从 ListBox1 重命名为 lstInvoice 并检查您的代码现在是否正常工作。

【讨论】:

    猜你喜欢
    • 2013-12-25
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-30
    相关资源
    最近更新 更多