WPF数据库空引用异常

时间:2017-03-26 13:17:50

标签: c# wpf database nullreferenceexception

我有以下数据库http://merc.tv/img/fig/Northwind_diagram.jpg,我正在创建一个WPF应用程序,当我点击一个员工时,它会显示该员工完成的订单。每当我运行代码时,我在这部分得到一个NullReferenceException:

public List<Order> orders {
    get { return selEmp.Orders.ToList(); }
}

这是我的WPF代码:

<Window x:Class="_ForPLUENorthwind1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:_ForPLUENorthwind1"
        xmlns:localn="clr-namespace:_ForPLUENorthwind1.model"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <ObjectDataProvider x:Key="vm" ObjectType="{x:Type localn:viewmodel}" />
    </Window.Resources>
    <Grid DataContext="{Binding Source={StaticResource vm}}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
            <ColumnDefinition Width="1*"/>
        </Grid.ColumnDefinitions>

        <ListBox x:Name="liemp" Grid.Column="0" Grid.Row="0"
                 ItemsSource="{Binding Allemp}"
                 DisplayMemberPath="FirstName"
                 SelectedValuePath="EmployeeID"
                 SelectedItem="{Binding Path=selEmp, Mode=TwoWay}"
                 />

        <ListBox x:Name="liorders" Grid.Column="1" Grid.Row="0" ItemsSource="{Binding orders}">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock>
                        <Run Text="{Binding OrderID}" />
                        <Run Text="{Binding OrderDate}" />
                    </TextBlock>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>

        <StackPanel Grid.Column="2" Grid.Row="0">
            <StackPanel>
                <TextBlock Text="" />
                <TextBox />
                <TextBlock Text="" />
                <TextBox />
                <TextBlock Text="" />
                <TextBox />
            </StackPanel>
            <Button x:Name="edit">Edit</Button>
            <Button x:Name="add" >Add</Button>
        </StackPanel>

    </Grid>
</Window>

这是我的viewmodel.cs:

class viewmodel : INotifyPropertyChanged
{
    NorthwindEntities db = new NorthwindEntities();
    public event PropertyChangedEventHandler PropertyChanged;
    private Employee _emp;

    public List<Employee> Allemp {
        get { return db.Employees.ToList(); }
    }

    public Employee selEmp {
        get { return _emp; }

        set {
            _emp = value;
            if (PropertyChanged != null) {
                PropertyChanged(this, new PropertyChangedEventArgs("orders"));
            }
        }
    }

    public List<Order> orders {
        get { return selEmp.Orders.ToList(); }
    }


}

第二个ListBox应包含订单

enter image description here

更新:当我使用调试模式浏览软件时,它运行并显示所有订单,但我仍然得到空引用

1 个答案:

答案 0 :(得分:0)

首先,将其用于setter

set {
        _emp = value;
        NotifyPropertyChange();

        if _emp != null
            NotifyPropertyChange("orders");
    }

NotifyPropertyChange应为

private void NotifyPropertyChange([CallerMemberName]string prop = null)
{
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
}

并将orders更改为:

public List<Order> orders {
    get { return (selEmp.Orders==null? null : selEmp.Orders.ToList()); }
}

看看此时是否收到NullReferenceException。即使这样,您也可能遇到绑定问题。您应该使用ObservableCollection进行绑定,而不是List