简体   繁体   English

VB.NET-更改Textbox的.Text属性会抛出Null Reference Exception

[英]VB.NET- Changing .Text property of Textbox throws Null Reference Exception

This MUST be trivial. 这必须是微不足道的。 I do this for a living and CAN NOT figure out why I am getting this exception: 我以此为生,无法弄清楚为什么我会得到这个例外:

System.NullReferenceException was unhandled Message=Object reference not set to an instance of an object. System.NullReferenceException未处理Message = Object引用未设置为对象的实例。

Here is the code: 这是代码:

Public Class frmMain

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim counties() As String

    counties = {"", "Huntsville, AL", "Madison, AL", "Rural Madison County, AL", "Taft, TN"}
    Me.cbCounties.DataSource = counties

    Me.lblStatus.Text = "[ Please select a county ]"
    Me.lblStatus.Left = Me.ClientSize.Width \ 2 - Me.lblStatus.Width \ 2



End Sub

Private Sub cbCounties_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbCounties.SelectedIndexChanged
    Select Case cbCounties.SelectedIndex
        Case 1
            txtTaxRate.Text = "8.00%" 'Issue is these, when index is changed.
        Case 2
            txtTaxRate.Text = "8.50%"
        Case 3
            txtTaxRate.Text = "5.50"
        Case 4
            txtTaxRate.Text = "9.50%"
        Case Else
            txtTaxRate.Text = Nothing
    End Select

    Me.lblStatus.Text = "[ Please enter net amount ]"
    Me.lblStatus.Left = Me.ClientSize.Width \ 2 - Me.lblStatus.Width \ 2

End Sub

End Class 结束班

Help? 救命?

Is this after you changed it a couple of times? 这是你改变它几次后?

Setting txtTaxRate = Nothing and later trying to set txtTaxRate.Text to something else will cause some problems. 设置txtTaxRate = Nothing以及稍后尝试将txtTaxRate.Text设置为其他内容将导致一些问题。

You are setting the textbox object to nothing, and later trying to reference one of its properties. 您正在将文本框对象设置为空,然后尝试引用其中一个属性。

Try changing 尝试改变

txtTaxRate = Nothing

to

txtTaxRate.Text = "" 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM