2012-04-05 192 views
0

我試圖根據他們的經驗水平(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 

謝謝!

回答

1

問題是使用Convert.ToInt32。該功能需要一個實現IConvertible接口的對象。否則它會拋出一個InvalidCastException

你想要做的就是將String解析爲Int32。與Int32.ParseInt32.TryParse一起使用效果最佳。如果您不確定輸入是否真的是十進制值,後者會很好。

那麼到底這是要你想做的事:

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

不是這樣。錯誤消息不是InvalidCastException – 2012-04-05 13:04:32

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 
相關問題