简体   繁体   中英

WPF database Null Reference Exception

I have the following database http://merc.tv/img/fig/Northwind_diagram.jpg and I'm making a WPF application that when I click on an employee, it shows me the orders done by that employee. Whenever I run the code, I get a NullReferenceException at this part:

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

This is my WPF code:

<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>

And this is my 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(); }
    }


}

The second ListBox should contain the Orders

在此输入图像描述

Update: When I go through the software using debug mode, it runs and shows all the orders, but I still get the null reference

First of all, use this for setter :

set {
        _emp = value;
        NotifyPropertyChange();

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

And the NotifyPropertyChange should be

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

And change the orders to:

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

See if you getting NullReferenceException at this point. Even then, you might have problems with the binding. You should use ObservableCollection for binding, not List .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM