2013-10-07 28 views
1

我有這種情況下數據已被加載到組合框和文本框,但當應用程序第一次被加載和組合框=菸草使用?並嘗試將文本框設置爲空,我得到「NullReferenceException - 對象引用未設置爲對象的實例」。不知道如何解決這個問題,並能夠在選擇「使用菸草?」時保持清除文本框。還有「菸草使用?」是組合框的默認值。如何解決NullReferenceException WPF文本框

組合框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; 
    } 
} 
+0

哪條線

if(cbTobacco != null) 

線是給你調試的錯誤?這將告訴我們在初始化之前你正在訪問哪個對象是null。 http://stackoverflow.com/a/4660186/2145211 – Harrison

+0

cbTobaccoCode.Text = strTobaccoCode;是我得到例外的地方。只有在應用第一次加載時纔會發生。 – Robert

回答

2

確定該應用程序在第一種情況下進入,並seted 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; 
    } 
} 
+0

仍然會收到「NullReferenceException - 對象引用未設置爲對象的實例」。在應用程序第一次加載時cbTobaccoCode.Text = strTobaccoCode – Robert

+0

它是Windows窗體還是WebForms? – rkawano

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

textBox.Text如何設置cbTobaccoCode.Text?發件人是cbTobacco.Text。 – Robert

0

嘗試添加這個(驗證如果文本框爲空,然後驗證該文本空)前

if (cbTobacco.Text != null)