2

I use WPF and have a combobox in Xaml:

<ComboBox Name="registerDateCboxReportSt"  SelectedIndex="0"
                            Height="30"  Width="91.478" 
                            HorizontalAlignment="Left" 
                            VerticalAlignment="Center"
                            Margin="99.323,3,0,3"
                            SelectionChanged="registerDateCboxReportSt_SelectionChanged" >
                            <ComboBoxItem Content="select Date:" Padding="5" />
                            <ComboBoxItem Content="On" Padding="5" />
                            <ComboBoxItem Content="Since" Padding="5" />
                        </ComboBox>

if user select "On" other comboboxes that show an specific date will be enabled. and if selects "Since" other comboboxes that represents strart date and finish date will be shown. C# code for that is:

    private void registerDateCboxReportSt_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        switch (registerDateCboxReportSt.SelectedIndex) 
        {
            case 0 :
                startDayReportSt.IsEnabled = startMonthReportSt.IsEnabled = startYearReportSt.IsEnabled = false;
                finishDayReportSt.IsEnabled = finishMonthReportSt.IsEnabled = finishYearReportSt.IsEnabled = false;
                break;
            case 1:
                startDayReportSt.IsEnabled = startMonthReportSt.IsEnabled = startYearReportSt.IsEnabled = true;
                finishDayReportSt.IsEnabled = finishMonthReportSt.IsEnabled = finishYearReportSt.IsEnabled = false;
                break;
            case 2:
                 startDayReportSt.IsEnabled = startMonthReportSt.IsEnabled = startYearReportSt.IsEnabled = true;
                finishDayReportSt.IsEnabled = finishMonthReportSt.IsEnabled = finishYearReportSt.IsEnabled = true;
                break;
        }
    }

when I start to run the application, the line after "case 0:" causes an error! this line:

startDayReportSt.IsEnabled = startMonthReportSt.IsEnabled = startYearReportSt.IsEnabled = false;

causes an error! error code is :

nullreferenceexception was unhandled by user code.

Object reference not set to an instance of an object.

why this line should cause error?!

Abolfazl
  • 45
  • 1
  • 7

1 Answers1

1

Try using x:Name=".." in your xaml instead of just Name=".."

<ComboBox x:Name="registerDateCboxReportSt" />

Do this for all elements you need access to in your code-behind.

x:Name="startDayReportSt" x:Name="startMonthReportSt" x:Name="startYearReportSt"

etc...

Laith
  • 6,071
  • 1
  • 33
  • 60