locked
Calculator code to calculate pay per piece. RRS feed

  • Question

  • Here is the code that I have. It keeps telling me there is a "NullReferenceUnhandled", it even tells me were I just can't seem to find the right fix. Any Help would be appreciated.


        Private Sub CalculatePayToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CalculatePayToolStripMenuItem.Click
    
            If TextBox1.Text <> "" Then
    
                Try
                    'Convert the quantity.
                    TextBox2.Text = Integer.Parse(TextBox2.Text)
    
                    'The number of pieces MUST be greater than zero.
                    If TotalPiecesInteger > 0 Then
                        'Determination of correct pay rate.
                        Select Case TotalPiecesInteger
                            Case Is >= 600
                                PayDecimal = PIECE_RATED_Decimal
                            Case 400 To 599
                                PayDecimal = PIECE_RATEC_Decimal
                            Case 200 To 399
                                PayDecimal = PIECE_RATEB_Decimal
                            Case 1 To 199
                                PayDecimal = PIECE_RATEA_Decimal
                        End Select
    
                        'Perform calculations.
    
                        PayEarnedDecimal = Convert.ToDecimal(PayDecimal * TotalPiecesInteger)
                        TotalPayDecimal += TotalPayDecimal
    
                        'Display this employee's pay.
                        Label3.Text = PayEarnedDecimal.ToString("C")
                        Label3.Text = TotalPayDecimal.ToString("C")
    
                        WorkerCountInteger += 1
                        TotalPiecesInteger += Integer.Parse(TextBox2.Text)
                    Else
                        'If say Zero was entered.
                        MessageBox.Show("Please enter a number greater than zero.")
                        MessageBoxButton.OK(MessageBoxIcon.Hand)
                    End If
    
                Catch QuantityException As FormatException
                    'The quantity MUST be numeric.
                    MessageBox.Show("Input Error")
                    MessageBoxButton.OK(MessageBoxIcon.Error)
                End Try
    
            Else
                'No entry in the name field.
                MessageBox.Show("Please enter an employee name.", "Missing Data.")
                MessageBoxButton.OK(MessageBoxIcon.Hand)
            End If
        End Sub

    I am just annoyed at this point and it might be obvious, I just don't see it.

    Sunday, May 18, 2014 10:12 PM

Answers

  • I guess the issue can be with values in the TextBox1 and TextBox2:

    • Check for the values in both the textboxes. You are checking for only TextBox1.
    • Check for null and empty instead of just empty string:
    If String.IsNullOrEmpty(TextBox1.Text) Then 
             Return "Text1 is null or empty" 
    If String.IsNullOrEmpty(TextBox2.Text) Then 
             Return "Text2 is null or empty" 
    
    

    TejasJ.

    • Marked as answer by Fred Bao Friday, May 30, 2014 8:58 AM
    Monday, May 19, 2014 5:01 AM

All replies

  • I guess the issue can be with values in the TextBox1 and TextBox2:

    • Check for the values in both the textboxes. You are checking for only TextBox1.
    • Check for null and empty instead of just empty string:
    If String.IsNullOrEmpty(TextBox1.Text) Then 
             Return "Text1 is null or empty" 
    If String.IsNullOrEmpty(TextBox2.Text) Then 
             Return "Text2 is null or empty" 
    
    

    TejasJ.

    • Marked as answer by Fred Bao Friday, May 30, 2014 8:58 AM
    Monday, May 19, 2014 5:01 AM
  • Thank you very helpful.
    Monday, June 2, 2014 4:57 AM