Archaios7
Archaios7

Reputation: 13

CollectionViewSource.View is null

Problem Solved - See bottom of this post - I keep getting a nullreferenceexception and can't figure out why, much less how to fix it. I have two comboboxes, selecting a value from one should populate the other with a relevant set of values.

    <CollectionViewSource x:Key="branchesViewSource"
            Source="{Binding Path=Branches, Source={StaticResource contactDBDataSet}}" />
    <CollectionViewSource x:Key="ranksViewSource"
            Source="{Binding Path=RankPath, Source={StaticResource contactDBDataSet}}" />

        private void loadBranches()
    {
        DocMan.ContactDBDataSet contactDBDataSet = ((DocMan.ContactDBDataSet)(this.FindResource("contactDBDataSet")));
        // Load data into the table Branches. You can modify this code as needed.
        DocMan.ContactDBDataSetTableAdapters.BranchesTableAdapter contactDBDataSetBranchesTableAdapter = new DocMan.ContactDBDataSetTableAdapters.BranchesTableAdapter();
        contactDBDataSetBranchesTableAdapter.Fill(contactDBDataSet.Branches);
        System.Windows.Data.CollectionViewSource branchesViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("branchesViewSource")));
        branchesViewSource.View.MoveCurrentToFirst();
    }

The above works just fine and populates the first combobox. This second block of code should populate the second combobox.

DocMan.ContactDBDataSet contactDBDataSet = ((DocMan.ContactDBDataSet)(this.FindResource("contactDBDataSet")));
            // Retrieve branchComboBox SelectedItem
            string currentItem = ((DataRowView)branchComboBox.SelectedItem)["Branch"].ToString();
            // Load data into table Ranks                 
            switch (currentItem)
            {
                case "Army":
                    rankPath = "ArmyRanks";
                    DocMan.ContactDBDataSetTableAdapters.ArmyRanksTableAdapter contactDBDataSetArmyRanksTableAdapter = new ContactDBDataSetTableAdapters.ArmyRanksTableAdapter();
                    contactDBDataSetArmyRanksTableAdapter.Fill(contactDBDataSet.ArmyRanks);
                    break;
                case "Navy":
                    rankPath = "NavyRanks";
                    DocMan.ContactDBDataSetTableAdapters.NavyRanksTableAdapter contactDBDataSetNavyRanksTableAdapter = new ContactDBDataSetTableAdapters.NavyRanksTableAdapter();
                    contactDBDataSetNavyRanksTableAdapter.Fill(contactDBDataSet.NavyRanks);
                    break;
                case blahblahblah and more cases

            }
            // Populate rankComboBox
            System.Windows.Data.CollectionViewSource ranksViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("ranksViewSource")));
            ranksViewSource.View.MoveCurrentToFirst();
        }

The nullreferenceexception is happening on the last line, basically telling me that ranksViewSource.View is null. Comparing to the first block of code, branchesViewSource.View is of type System.Windows.Data.BindingListCollectionView. Any help or insight is much appreciated in advance.

_----_----

I needed to create a new Binding and set the Source & Path of the binding, then apply the SetBinding() method of my combobox as appropriate.

DocMan.ContactDBDataSet contactDBDataSet = ((DocMan.ContactDBDataSet)(this.FindResource("contactDBDataSet")));
            // Create binding
            Binding rankBinding = new Binding();
            // Retrieve branchComboBox SelectedItem
            string currentItem = branchComboBox.SelectedValuePath;
            // Load data into table Ranks                 
            switch (currentItem)
            {
                case "Army":
                    rankBinding.Path = new PropertyPath("ArmyRanks");
                    rankBinding.Source = this.FindResource("rankViewSource");
                    rankComboBox.SetBinding(ComboBox.ItemsSourceProperty, rankBinding);
                    DocMan.ContactDBDataSetTableAdapters.ArmyRanksTableAdapter contactDBDataSetArmyRanksTableAdapter = new ContactDBDataSetTableAdapters.ArmyRanksTableAdapter();
                    contactDBDataSetArmyRanksTableAdapter.Fill(contactDBDataSet.ArmyRanks);
                    // Populate rankComboBox
                    CollectionViewSource armyRanksViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("rankViewSource")));
                    armyRanksViewSource.View.MoveCurrentToFirst();

Upvotes: 0

Views: 1950

Answers (2)

Chris Bargh
Chris Bargh

Reputation: 327

I know this is a late response but, if my experience can save someone going through the couple of days of pain that I went through, then I feel it is of value to share my experiences...

I had the same error occur after this area of my code had been running faultlessly for some time. It turned out that, after a necessary rebuild of my database, I did not reload trial data into one of my tables. Because the table was empty, FindResource returned a valid CollectionViewSource but its View property was null.

My simple fix was to alter the automatically generated code to test for this and to not call MoveCurrentToFirst() if View is null:

CollectionViewSource myViewSource = ((CollectionViewSource)(FindResource("myViewSource")));
if (myViewSource.View != null)
    myViewSource.View.MoveCurrentToFirst();

@Archaios7, I must admit I do not understand why you said that you "needed to create a new Binding and set the Source & Path of the binding, then apply the SetBinding() method of my combobox" so I don't know whether your solution is better than mine. (I thank anyone who has a burning need to explain this to me :-)

I think the importance of my comments is more about the cause of the problem - which, I admit, quite surprised me - than about my solution.

Upvotes: 1

Kevin DiTraglia
Kevin DiTraglia

Reputation: 26058

I am speculating a bit because some of the syntax for this is a bit unfamiliar to me, but I believe what is happening is this...

<CollectionViewSource x:Key="branchesViewSource"
        Source="{Binding Path=Branches, Source={StaticResource contactDBDataSet}}" />
                           //  ^  ^ ---------------------------------------
contactDBDataSetBranchesTableAdapter.Fill(contactDBDataSet.Branches);//   |
                           //                                 ^  ^--------|
                           //             path matches so view is populated

<CollectionViewSource x:Key="ranksViewSource"
        Source="{Binding Path=RankPath, Source={StaticResource contactDBDataSet}}" />
        //                     ^  ^---------------------------------------|
contactDBDataSetArmyRanksTableAdapter.Fill(contactDBDataSet.ArmyRanks);// |
        //                                                     ^  ^-------|
        //                           These don't match so from code posted
        //                           view for ranksViewSource is still null   

At some point you need something to populate RankPath (it's unclear if this ever happens from the code posted).

Upvotes: 0

Related Questions