locked
Runtime Errors RRS feed

  • Question

  • User17632381 posted

    Hi There, I can't seem to get rid of these errors, this code basically validates Gender against Title to make sure that you can't select a Male Title with a Female Gender and vise versa. If I don't use the If Not statements then the program just crashes telling me that theres a null value. So if I could get any help to fix this, it would be greatly appriatiated.

    Dim titlePresenceValidate As Boolean = True 'Title Validation Variable
    
    Dim genderPresenceValidate As Boolean = True 'Gender Validation Variable
    
    Dim genderVerific As Boolean = True 'Gender against Title Verification Variable
    Dim titleString As String 'Gender against Title Verification Variable
    Dim genderString As String 'Gender against Title Verification Variable
    
    If Not titleComboBox.SelectedItem = Nothing Then 'Validation Code For Title Combo Box
    titleString = titleComboBox.SelectedItem.ToString
    titlePresenceValidate = True
    titleAsterisk.Visible = False
    Else
    titlePresenceValidate = False
    MsgBox("Check that you have selected a Title", MsgBoxStyle.Critical)
    titleAsterisk.Visible = True
    End If
    
    If Not genderComboBox.SelectedItem = Nothing Then
    genderString = genderComboBox.SelectedItem.ToString
    genderPresenceValidate = True
    genderAsterisk.Visible = False
    Else
    genderPresenceValidate = False
    MsgBox("Check that you have selected a Gender", MsgBoxStyle.Critical)
    genderAsterisk.Visible = True
    End If
    
    If titleString = "Mr" And genderString = "Male" Then 'Male Verification Technique
    genderVerific = True
    titleAsterisk.Visible = False
    genderAsterisk.Visible = False
    ElseIf titleString = "Sir" And genderString = "Male" Then 'Male Verification Technique
    genderVerific = True
    titleAsterisk.Visible = False
    genderAsterisk.Visible = False
    ElseIf titleString = "Miss" And genderString = "Female" Then 'Female Verification Technique
    genderVerific = True
    titleAsterisk.Visible = False
    genderAsterisk.Visible = False
    ElseIf titleString = "Mrs" And genderString = "Female" Then 'Female Verification Technique
    genderVerific = True
    titleAsterisk.Visible = False
    genderAsterisk.Visible = False
    ElseIf titleString = "Ms" And genderString = "Female" Then 'Female Verification Technique
    genderVerific = True
    titleAsterisk.Visible = False
    genderAsterisk.Visible = False
    Else
    genderVerific = False
    MsgBox("The Gender and the Title of the customer do not match, please check your entry and try again", MsgBoxStyle.Critical)
    titleAsterisk.Visible = True
    genderAsterisk.Visible = True
    End If

    Errors I get:

    Warning 1 Variable 'titleString' is used before it has been assigned a value. A null reference exception could result at runtime.

    Warning 2 Variable 'genderString' is used before it has been assigned a value. A null reference exception could result at runtime.

    Sunday, February 3, 2013 11:23 AM

Answers

  • User-1716253493 posted
    Dim titleString As String =""
    Dim genderString As String =""

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 3, 2013 11:32 AM

All replies

  • User-1716253493 posted
    Dim titleString As String =""
    Dim genderString As String =""

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 3, 2013 11:32 AM
  • User17632381 posted

    Thanks so much, I should have seen that, I'm not having a good day today programming wise :(

    Sunday, February 3, 2013 11:39 AM