简体   繁体   English

C#程序崩溃comboBox SelectedItem ToString

[英]C# Program Crash comboBox SelectedItem ToString

I have no idea, why my program crash. 我不知道为什么我的程序崩溃了。

If i click "Reload" Button: 如果我单击“重新加载”按钮:

private void reloadBtn_Click(object sender, RoutedEventArgs e)
{
    comboFilter.Items.Clear();
    dataGridPrivatecustomers.Columns.Clear();
    dataGridPrivatecustomers.ItemsSource = null;
    load_columns_privatecustomer();
    load_values_privatecustomer();
}

All works. 所有作品。 But if I select a filter for my search function and click reload, then it crashes: 但是,如果我为搜索功能选择一个过滤器,然后单击“重新加载”,则会崩溃:

private void comboFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    labelfilter.Content = "Filter: " + comboFilter.SelectedItem;
    filtervalue = comboFilter.SelectedItem.ToString();
}

This is the breakpoint: 这是断点:

filtervalue = comboFilter.SelectedItem.ToString();

And I get a NulLReferenceException Error. 而且我收到了NulLReferenceException错误。 I tryed to make a filtervalue = null; 我试图使filtervalue = null; in reloadBtn_Click but also doesn't work. 在reloadBtn_Click中,但也不起作用。

The comboFilter_SelectionChanged is somehow fired after the reload where you remove items from the combo , which is result of clear method. 重新加载后,您会以某种方式触发comboFilter_SelectionChanged ,这是您从combo删除项目的原因,这是clear方法的结果。 Make sure you have SelectedItem not null in comboFilter_SelectionChanged before you use it. 使用它之前,请确保comboFilter_SelectionChanged SelectedItem不为null。

private void comboFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if( comboFilter.SelectedItem != null)
    {
        labelfilter.Content = "Filter: " + comboFilter.SelectedItem;
        filtervalue = comboFilter.SelectedItem.ToString();
    }
}

As a additional note your program must not crash by not catching the exception being thrown in your program. 另外要注意的是,您的程序一定不要因未捕获程序中引发的异常而崩溃。 Use try-catch to properly handle the exception. 使用try-catch可以正确处理异常。 And also try to avoid them before they could occur. 并且也要尽量避免它们发生。 Like we did here by checking for null. 就像我们在这里检查null一样。 This will prevent program crashing. 这样可以防止程序崩溃。

try-catch (C# Reference) - Why program would crash (stop execution) try-catch(C#参考) -为什么程序会崩溃(停止执行)

When an exception is thrown, the common language runtime (CLR) looks for the catch statement that handles this exception. 引发异常时,公共语言运行库(CLR)查找处理该异常的catch语句。 If the currently executing method does not contain such a catch block, the CLR looks at the method that called the current method, and so on up the call stack. 如果当前执行的方法不包含这样的catch块,则CLR将查看调用当前方法的方法,依此类推。 If no catch block is found, then the CLR displays an unhandled exception message to the user and stops execution of the program. 如果未找到catch块,则CLR向用户显示未处理的异常消息,并停止执行程序。

The exception gets thrown because comboFilter_SelectionChanged is called implicitly from reloadBtn_Click when comboFilter.Items.Clear() is called. 唯一的例外得到投掷,因为comboFilter_SelectionChanged从隐式调用reloadBtn_ClickcomboFilter.Items.Clear()被调用。 In this case comboFilter.SelectedItem changes from the previously selected item to null . 在这种情况下, comboFilter.SelectedItem从先前选择的项目更改为null

Check for null in comboFilter_SelectionChanged : 检查comboFilter_SelectionChanged null

private void comboFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (comboFilter.SelectedItem != null)
    {
        labelfilter.Content = "Filter: " + comboFilter.SelectedItem;
        filtervalue = comboFilter.SelectedItem.ToString();
    }
}

您可能想为comboFilter.SelectedItem添加一个空检查,例如,像comboFilter.SelectedItem?.ToString()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM