如何修复NullReferenceException WPF文本框

时间:2013-10-07 14:39:09

标签: c# wpf c#-4.0 wpf-controls

我有这种情况,在数据加载到组合框和文本框后工作,但是当应用程序首次加载并且组合框是=烟草使用?和trys将文本框设置为空我得到“NullReferenceException - 对象引用未设置为对象的实例”。当选择“Tobacco Use?”时,不确定如何解决此问题并能够保持清除文本框。还有“烟草使用?”是组合框的默认值。

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;
  }
}

3 个答案:

答案 0 :(得分:2)

您确定在第一个案例中输入了应用程序,并设置了strTobaccoCode的值吗?放一个断点并检查。

使用“默认”选项让您的代码更安全一些:

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;
    }
}

答案 1 :(得分:1)

您也应该设置默认情况。另外,你为什么直接访问cbTobacco?您应该将发件人(cbTobacco,如果您已正确连接事件)转换为文本框并从中访问值:

 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 ?? "";
  }
}

答案 2 :(得分:0)

尝试添加此项(验证文本框是否为null,然后验证文本是否为空)

    if(cbTobacco != null)

之前的

if (cbTobacco.Text != null)