views:

104

answers:

2

Having been given the all clear to move code from my Page_Loaded method to the constructor (See HERE), i am now encountering errors on my Linq to entities query. It is now causing a nullreferenceexception and i can't figure out why at the moment. See below for the exception location.

public Building()
{
    InitializeComponent();

    lvBuildings.ItemsSource = App.ocBuildings;
    getBuildings();
}


private void Page_Loaded(object sender, RoutedEventArgs e)
{

}

private void getBuildings()
{
    App.ocBuildings.Clear();
    var tehBuildings = from building in App.ents.Buildings
                       where building.Organisations.OrganisationID == App.selectedOrganisation.OrganisationID
                       select building;

    foreach (Buildings addBuilding in tehBuildings (<--Exception))
    {
        App.ocBuildings.Add(addBuilding);
    }

}

Anyone have any ideas?

Thanks, Kohan.

+3  A: 

It looks like one of the following items is evaluating to null

  • App.ents
  • App.ents.Buildings
  • building.Organisations
  • App.SelectedOrganisation

This would cause a NullReferenceException to be thrown in the for each loop because the query is not actually evaluated until it is used.

We'll need some more information from you as to which one is null.

JaredPar
It looks like App.selectedOrganisation is null. Why would moving this code from Page_Loaded (where it works fine) to the constructor cause it to break?
Kohan
@Kohan, it's likely some other code which runs between the constructor and Page_Loaded event is causing this value to be non-null. I don't have enough context into your project though to responsibly say what though.
JaredPar
Page_Loaded fires after the page was loaded. App.selectedOrganization is almost certainly a reference to something that gets set as the result of binding. The binding hasn't occurred before the page is loaded.
Robert Rossney
A: 

@JaredPar.

Fixed thanks to you, i would have never thought to look at the previous page in my client (that i was loading from) for the answer.

I had a button with the code:

        Page newPage;
        if (App.ModeType == "Mode1"){ newPage = new MyClient.Pages.Mode1.Building(); }
        else if (App.ModeType == "Mode2") { newPage = new MyClient.Pages.Mode2.RiskQuestions(); }
        else { throw new NotImplementedException(); } ///Must be Mode3

            Organisations thisOrg = (Organisations)lvOrganisations.SelectedItem;
        App.selectedOrganisation = thisOrg;

        NavigationService.Navigate(newPage);

By moving App.selectedOrganisation to before i set the "newPage" everything was fixed. I guess that the constructor gets called at the point i was setting "newPage".

For the sake of clarity, Final code which worked for me looks like this:

            Organisations thisOrg = (Organisations)lvOrganisations.SelectedItem;
        App.selectedOrganisation = thisOrg;

        Page newPage;
        if (App.ModeType == "Mode1"){ newPage = new MyClient.Pages.Mode1.Building(); }
        else if (App.ModeType == "Mode2") { newPage = new MyClient.Pages.Mode2.RiskQuestions(); }
        else { throw new NotImplementedException(); } ///Must be Mode3

        NavigationService.Navigate(newPage);
Kohan