错误对象引用未设置为对象的实例

时间:2012-04-05 12:44:00

标签: vb.net

我试图根据他们的经验水平(1-4)作为整数获得销售员奖金的计算,并且他们以十进制的形式销售(1-10000)。为什么我现在收到这个错误?这是代码......

Public Class Form1

    Dim intLvlsTextBox1Numbers As String = Nothing And intLvlsTextBox1Numbers = txtBoxLvl.Text
    Dim decSaleWkTextBox1Numbers As String = Nothing And decSaleWkTextBox1Numbers = txtBoxWklySale.Text

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
        'The Try/Catch method is used to catch the "InvalidCastExceptionUnhandeled" in order to use the 
        'exception for detecting letters within the textboxes being understood in logic as of
        'a wrong type, thus enabling its detection as letter or caracter.
        Try
            intLvlsTextBox1Numbers = Convert.ToInt32(txtBoxLvl.Text)
            decSaleWkTextBox1Numbers = Convert.ToInt32(txtBoxWklySale.Text)
            'This line is used to validate indetify non-valid data input upon entring numbers
            'that are out of rang, and will display the warning error message. It goes from 
            'anything not convertable to Integer 32, i.e. letter, signs.
            MessageBox.Show("Please, input a numerical value")
            'Reset the cursor position when a non-valid data is inputed.
            txtBoxLvl.Select()
            'Catches the EX variable execption,"InvalidCastExceptionUnhandeled".
        Catch ex As FormatException

        End Try

        'procedure call to the ChoiceMenus
        ChoiceMenus()

    End Sub
    Private Sub ChoiceMenus()
        Select Case intLvlsTextBox1Numbers
            Case 1 To 4
        End Select
        Select Case decSaleWkTextBox1Numbers
            Case 1 To 100000
        End Select
    End Sub
    Private Sub isValidCalculation()
        lblTotWkSale.Text = 250 * (intLvlsTextBox1Numbers + 1) + ((intLvlsTextBox1Numbers + 1) / 100) * (decSaleWkTextBox1Numbers)
        decSaleWkTextBox1Numbers = Convert.ToString(lblTotWkSale.Text)
        lblTotWkSale.Text = decSaleWkTextBox1Numbers.ToString("c")
    End Sub
    Private Sub btnClr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClr.Click

        clearForm() 'procedure call

    End Sub

    Private Sub clearForm()
        txtBoxWklySale.Text = ""
        txtBoxLvl.Text = ""
        lblTotWkSale.Text = ""
        lblErrorMsg.Text = ""
        txtBoxLvl.Select()
        'tbxHome = True
        'tbx1 = True
        'tbx2 = True
    End Sub

    Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

        Me.Close() 'closes the application
    End Sub
End Class

谢谢!

2 个答案:

答案 0 :(得分:1)

问题在于使用Convert.ToInt32。此函数需要一个实现IConvertible接口的对象。否则它会抛出InvalidCastException

您要做的是将String解析为Int32。通过Int32.ParseInt32.TryParse做最佳效果。如果您不确定输入是否真的是十进制值,后者效果很好。

所以最后这是你想做的事情:

intLvlsTextBox1Numbers = Int32.Parse(txtBoxLvl.Text)
decSaleWkTextBox1Numbers = Int32.Parse(txtBoxWklySale.Text)

答案 1 :(得分:1)

 Dim intLvlsTextBox1Numbers As String = Nothing And intLvlsTextBox1Numbers = txtBoxLvl.Text

初始化字段的方式非常有创意。虽然我无法理解它应该做什么。运行时也没有,当表单的构造函数运行时,它会用NRE轰炸你的程序。难以诊断,因为调试器没有很好的线来向您展示。它失败是因为txtBoxLvl变量尚未初始化,直到稍后,在InitializeComponent()运行之后才会发生。可悲的是,vb.net也隐藏了一些东西。

尝试写一些更健全的代码,你可以在一本关于vb.net编程的好书中找到它。这些变量根本不应该是表单类中的字段,它们应该是方法中的局部变量:

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
     Dim intLvlsTextBox1Numbers As Integer '' It is not a string!!
     Dim levelOk = Integer.TryParse(txtBoxLvl.Text, intLvlsTextBox1Numbers)
     If Not levelOk Then
         '' complain...
         Return
     End If
     '' etc...
End Sub