E
E
Evgeny Ivanov2021-11-19 11:22:13
WPF
Evgeny Ivanov, 2021-11-19 11:22:13

How to fix System.NullReferenceException error?

There are 2 wpf components on the form

ComboBox Name =  "ShowThisMessageToComboBox"   SelectionChanged = "ShowThisMessageToComboBox_Changed"
TextBox Name = "ProgrammIdsTextBox"


It is necessary to hide the textbox when selecting item 0 in the combobox, and show it when selecting 1.

Here is the code
private void ShowThisMessageToComboBox_Changed(object sender, SelectionChangedEventArgs e)
        {
            if (ShowThisMessageToComboBox.SelectedIndex == 0) ProgrammIdsTextBox.Visibility = Visibility.Hidden; // Ошибка на этой строке
            if (ShowThisMessageToComboBox.SelectedIndex == 1) ProgrammIdsTextBox.Visibility = Visibility.Visible;
        }


As a result, I get the error System.NullReferenceException: 'Object reference not set to an instance of an object.'
Most likely there is no reference to the object.

PS
The similar logic worked in WinForms.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Davilkus Games, 2021-11-22
@Davilkus

Check if you have a TextBox in a container? Then his program will not see.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question