From 944818bd4861f5bac10b4104cf1a4a75836d182a Mon Sep 17 00:00:00 2001 From: Paul Hebble Date: Sun, 2 Feb 2020 18:10:02 +0000 Subject: [PATCH] Fix null reference in recommendations --- GUI/Controls/ChooseRecommendedMods.cs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) 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;