如何修复空引用异常WPF文本框
本文关键字:WPF 文本 异常 引用 何修复 | 更新日期: 2023-09-27 18:34:10
我有这种情况,它在将数据加载到组合框和文本框后起作用,但是当应用程序首次加载并且组合框是 = 烟草使用? 并尝试将文本框设置为空,我得到"空引用异常 - 对象引用未设置为对象的实例"。 不确定如何解决此问题,并在选择"烟草使用? "被选中时,不知道如何解决这个问题并能够保持它清除文本框。此外,"烟草使用?"是组合框上的默认值。
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;
}
}
您确定应用程序在第一个案例中输入并设置了 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;
}
}
您还应该设置默认大小写。另外,你为什么直接访问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 ?? "";
}
}
尝试添加此内容(验证文本框是否为空,然后验证文本是否为空)
if(cbTobacco != null)
行之前
if (cbTobacco.Text != null)