2

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. I tryed to make a filtervalue = null; in reloadBtn_Click but also doesn't work.

Adil
  • 146,340
  • 25
  • 209
  • 204
O Jean
  • 105
  • 9
  • Have you tried surrounding your code with a try catch clause to get more information about your error? – Mighty Badaboom Mar 16 '17 at 09:34
  • 1
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Sinatr Mar 16 '17 at 09:35

3 Answers3

4

The comboFilter_SelectionChanged is somehow fired after the reload where you remove items from the combo, which is result of clear method. Make sure you have SelectedItem not null in comboFilter_SelectionChanged before you use it.

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. And also try to avoid them before they could occur. Like we did here by checking for null. This will prevent program crashing.

try-catch (C# Reference) - Why program would crash (stop execution)

When an exception is thrown, the common language runtime (CLR) looks for the catch statement that handles this exception. 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. If no catch block is found, then the CLR displays an unhandled exception message to the user and stops execution of the program.

Adil
  • 146,340
  • 25
  • 209
  • 204
1

The exception gets thrown because comboFilter_SelectionChanged is called implicitly from reloadBtn_Click when comboFilter.Items.Clear() is called. In this case comboFilter.SelectedItem changes from the previously selected item to null.

Check for null in comboFilter_SelectionChanged:

private void comboFilter_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (comboFilter.SelectedItem != null)
    {
        labelfilter.Content = "Filter: " + comboFilter.SelectedItem;
        filtervalue = comboFilter.SelectedItem.ToString();
    }
}
Dmitry Egorov
  • 9,542
  • 3
  • 22
  • 40
0

You may want to add a null-check for comboFilter.SelectedItem, e.g. like comboFilter.SelectedItem?.ToString()

Thomas Hahn
  • 332
  • 4
  • 13