diff --git a/CHANGELOG.md b/CHANGELOG.md index 848ceb09b8..3094ec553d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ All notable changes to this project will be documented in this file. ### Features ### Bugfixes +- [GUI] Fix null reference in recommendations (#2984 by: HebaruSan; reviewed: politas) ### Internal diff --git a/GUI/Controls/ChooseRecommendedMods.cs b/GUI/Controls/ChooseRecommendedMods.cs index 62a8c57a79..949579265b 100644 --- a/GUI/Controls/ChooseRecommendedMods.cs +++ b/GUI/Controls/ChooseRecommendedMods.cs @@ -64,11 +64,16 @@ private void RecommendedModsListView_SelectedIndexChanged(object sender, EventAr OnSelectedItemsChanged(RecommendedModsListView.SelectedItems); } } - + private void RecommendedModsListView_ItemChecked(object sender, EventArgs e) { var conflicts = FindConflicts(); - foreach (var item in RecommendedModsListView.Items.Cast()) + foreach (var item in RecommendedModsListView.Items.Cast() + // Apparently ListView handes AddRange by: + // 1. Expanding the Items list to the new size by filling it with nulls + // 2. One by one, replace each null with a real item and call _ItemChecked + // ... so the Items list can contain null!! + .Where(it => it != null)) { item.BackColor = conflicts.ContainsKey(item.Tag as CkanModule) ? Color.LightCoral @@ -94,7 +99,7 @@ private Dictionary FindConflicts() return new RelationshipResolver( RecommendedModsListView.CheckedItems.Cast() .Select(item => item.Tag as CkanModule) - .Distinct(), + .Distinct(), new CkanModule[] { }, conflictOptions, registry, kspVersion ).ConflictList;