1

I'm getting a NullReferenceException in my code in the call to checkBox2_Checked. The exception indicates that stackPanelListbox is null. It's declared in the XAML, and a similarly declared stackPanel is not null. What is wrong here?

Here's the XAML:

<Window x:Class="ch0103.WPF.LayoutWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="LayoutWindow" Height="450" Width="900">
    <StackPanel Name="stackPanelMain">
        <WrapPanel>
            <CheckBox Name="checkBox1" VerticalAlignment="Center" IsThreeState="False"     IsChecked="True" Click="checkBox_Checked" Content="Button StackPanel" Margin="0,0,11,0" />
            <CheckBox Content="Listbox StackPanel" Height="16" Name="checkBox2" IsChecked="True" Checked="checkBox2_Checked" />
        </WrapPanel>
        <Grid Name="grid1" ShowGridLines="True">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <StackPanel HorizontalAlignment="Left" Name="stackPanelButtons" VerticalAlignment="Top" Visibility="Visible">
                <Button Content="Button" Height="23" Name="button1" />
                <Button Content="Button" Height="23" Name="button2" />
                <Button Content="Button" Height="23" Name="button3" />
            </StackPanel>
            <StackPanel Name="stackPanelListbox" Grid.Column="1">
                <ListBox Grid.Column="2" HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Top" Width="200">
                    <ListBoxItem Content="Test" />
                    <ListBoxItem Content="Test" />
                    <ListBoxItem Content="Test" />
                    <ListBoxItem Content="Test" />
                    <ListBoxItem Content="Test" />
                    <ListBoxItem Content="Test" />
                    <ListBoxItem Content="Test" />
                </ListBox>
            </StackPanel>
        </Grid>
    </StackPanel>
</Window>

Here's the c# code:

using System.Windows;

namespace ch0103.WPF
{
    /// <summary>
    /// Interaction logic for LayoutWindow.xaml
    /// </summary>
    public partial class LayoutWindow : Window
    {
        public LayoutWindow() {
            InitializeComponent();
        }

        private void checkBox_Checked(object sender, RoutedEventArgs e) {
            stackPanelButtons.Visibility = (bool) checkBox1.IsChecked ? 
                Visibility.Visible : Visibility.Collapsed;
        }

        private void checkBox2_Checked(object sender, RoutedEventArgs e) {
            stackPanelListbox.Visibility = (bool) checkBox2.IsChecked ? // stackPanelListbox is null here? 
                Visibility.Visible : Visibility.Collapsed;
        }
    }
}
H.B.
  • 166,899
  • 29
  • 327
  • 400
jdphenix
  • 15,022
  • 3
  • 41
  • 74

4 Answers4

1

I think checkBox2.IsChecked is null.
Try this:

stackPanelListbox.Visibility = (checkBox2.IsChecked ?? false) ?
                               Visibility.Visible : Visibility.Collapsed;
Daniel Hilgarth
  • 171,043
  • 40
  • 335
  • 443
1

When the exception is thrown? at the startup of the window, or after you press the CheckBox?

If it's in at the startup, maybe it is because the checkBox2_Checked() is called during initializecompenents(), while stackPanelListbox is not yet declared.

Talke
  • 151
  • 6
  • The exception happens during the startup of the window. I didn't think the event could happen until the user actually clicked on it. I'll null check the method and get back with you – jdphenix Sep 05 '11 at 08:04
  • This was exactly it. For some reason Checkbox.Checked is fired during initializeComponent(), and Checkbox.Clicked isn't, so I changed it to that. – jdphenix Sep 05 '11 at 08:11
1

If you only want to hide or display the StackPanel in your event handler, you could use data binding instead:

<Window x:Class="ch0103.WPF.LayoutWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <StackPanel Name="stackPanelMain">
        <WrapPanel>
            <!-- Note the removed event handlers here: -->
            <CheckBox Name="checkBox1" VerticalAlignment="Center" IsThreeState="False" IsChecked="True" Content="Button StackPanel" Margin="0,0,11,0" />
            <CheckBox Content="Listbox StackPanel" Height="16" Name="checkBox2" IsChecked="True" IsThreeState="False" />
        </WrapPanel>
        <Grid Name="grid1" ShowGridLines="True">
            <!-- This one is used to convert checkbox state to visibility -->
            <Grid.Resources>
                <BooleanToVisibilityConverter x:Key="BoolToVisConverter"/>
            </Grid.Resources>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto" />
                <ColumnDefinition />
            </Grid.ColumnDefinitions>
            <!-- Additional Binding attributes in both StackPanel elements: -->
            <StackPanel HorizontalAlignment="Left" Name="stackPanelButtons" VerticalAlignment="Top" Visibility="{Binding IsChecked, ElementName=checkBox1, Converter={StaticResource BoolToVisConverter}}">
                <Button Content="Button" Height="23" Name="button1" />
                <Button Content="Button" Height="23" Name="button2" />
            </StackPanel>
            <StackPanel Name="stackPanelListbox" Grid.Column="1" Visibility="{Binding IsChecked, ElementName=checkBox2, Converter={StaticResource BoolToVisConverter}}">
                <ListBox HorizontalAlignment="Left" Name="listBox1" VerticalAlignment="Top" Width="200">
                    <ListBoxItem Content="Test" />
                    <ListBoxItem Content="Test" />
                </ListBox>
            </StackPanel>
        </Grid>
    </StackPanel>
</Window>

This approach allows you to remove the event handlers. So you would not run into initialization order errors.

Stephan
  • 4,187
  • 1
  • 21
  • 33
0

Try to remove Grid.Column = 2 from the xaml in ListBox.

luviktor
  • 2,240
  • 2
  • 22
  • 23