1

i have this case which works after data has been loaded to to the combobox and the textbox, but when the app first gets loaded and the combobox is = Tobacco Use? and trys to set the textbox to empty i get "NullReferenceException - Object reference not set to an instance of an object". not sure how to fix this and be able to keep it clearing the textbox when "Tobacco Use? " is selected. Also "Tobacco Use?" is the default on the combobox.

comboBox cbTobacco.Text

textBox = cbTobaccoCode.Text

private void cbTobacco_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
   if (cbTobacco.Text != null)
   {
     switch (Convert.ToString(cbTobacco.Text))
      {
        case "Tobacco Use?": strTobaccoCode = ""; break;
        case "1 - Current every day smoker": strTobaccoCode = "449868002"; break;
        case "2 - Current some day smoker": strTobaccoCode = "428041000124106"; break;
        case "3 - Former smoker": strTobaccoCode = "8517006"; break;
        case "4 - Never smoker": strTobaccoCode = "266919005"; break;
        case "5 - Smoker, current status unknown": strTobaccoCode = "77176002"; break;
        case "6 - Unknown if ever smoked": strTobaccoCode = "266927001"; break;
        case "7 - Heavy tobacco smoker": strTobaccoCode = "428071000124103"; break;
        case "8 - Light tobacco smoker": strTobaccoCode = "428061000124105"; break;
      }
  cbTobaccoCode.Text = strTobaccoCode;
  }
}
Robert
  • 646
  • 4
  • 12
  • 37
  • Which line is giving you the error on debug? That will tell us which object is a null that you are accessing before initializing it. http://stackoverflow.com/a/4660186/2145211 – Harrison Oct 07 '13 at 14:48
  • cbTobaccoCode.Text = strTobaccoCode; is where i am getting the exception. and it only happens when the app first loads up. – Robert Oct 07 '13 at 14:55

3 Answers3

2

Are you sure that application entered on first case, and seted the value of strTobaccoCode? Put a breakpoint and check.

Use the "default" option to make your code a little safer:

private void cbTobacco_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (cbTobacco.Text != null)
    {
        string strTobaccoCode;
        switch (Convert.ToString(cbTobacco.Text))
        {
            case "1 - Current every day smoker": strTobaccoCode = "449868002"; break;
            case "2 - Current some day smoker": strTobaccoCode = "428041000124106";     break;
            case "3 - Former smoker": strTobaccoCode = "8517006"; break;
            case "4 - Never smoker": strTobaccoCode = "266919005"; break;
            case "5 - Smoker, current status unknown": strTobaccoCode = "77176002"; break;
            case "6 - Unknown if ever smoked": strTobaccoCode = "266927001"; break;
            case "7 - Heavy tobacco smoker": strTobaccoCode = "428071000124103"; break;
            case "8 - Light tobacco smoker": strTobaccoCode = "428061000124105"; break;
            default: strTobaccoCode = ""; break;
        }
        cbTobaccoCode.Text = strTobaccoCode;
    }
}
rkawano
  • 2,443
  • 22
  • 22
  • still getting the "NullReferenceException - Object reference not set to an instance of an object". on the cbTobaccoCode.Text = strTobaccoCode when the app first loads up – Robert Oct 07 '13 at 15:05
  • Is it a Windows Forms or WebForms? – rkawano Oct 07 '13 at 16:21
1

You should set a default case as well. Also, why are you directly accessing cbTobacco? You should cast the sender (cbTobacco if you've wired up your event correctly) to a text box and access values from there:

 private void cbTobacco_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
   var textBox = sender as TextBox;

   if (textBox != null && textBox.Text != null)
   {
     switch (Convert.ToString(textBox.Text))
      {
        case "Tobacco Use?": strTobaccoCode = ""; break;
        case "1 - Current every day smoker": strTobaccoCode = "449868002"; break;
        case "2 - Current some day smoker": strTobaccoCode = "428041000124106"; break;
        case "3 - Former smoker": strTobaccoCode = "8517006"; break;
        case "4 - Never smoker": strTobaccoCode = "266919005"; break;
        case "5 - Smoker, current status unknown": strTobaccoCode = "77176002"; break;
        case "6 - Unknown if ever smoked": strTobaccoCode = "266927001"; break;
        case "7 - Heavy tobacco smoker": strTobaccoCode = "428071000124103"; break;
        case "8 - Light tobacco smoker": strTobaccoCode = "428061000124105"; break;
        default: strTobaccoCode = ""; break;
      }

      textBox.Text = strTobaccoCode ?? "";
  }
}
ramsey_tm
  • 792
  • 5
  • 7
0

Try add this (verify if the textbox is null then verify if the text is null)

    if(cbTobacco != null)

line before the

if (cbTobacco.Text != null)
Alexandre
  • 498
  • 2
  • 12