The following code has only thrown a NullReferenceException a handful of times over the last several months, but I'm not exactly sure why. The code isn't mine, but it looks pretty straight forward to me.
Type pageType = page.GetType();
if (_pages.TryGetValue(pageType, out value))
return value;
// The following line throws the exception
return _pages[pageType] = new MyPage(_section.Pages[page]);
[NullReferenceException: Object reference not set to an instance of an object.] System.Collections.Generic.Dictionary
2.Insert(TKey key, TValue value, Boolean add) +210 System.Collections.Generic.Dictionary
2.set_Item(TKey key, TValue value) +11
The only thing I can think of is that pageType
is null when it's being used as a dictionary key, but apparently that is not possible.
The code that calls it is simple:
protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
_mypage = GetPage();
}
I also thought that the error might be with the _section.Pages
, but section is never null and never sets anything. If .Pages[page]
returns null, the MyPage
constructor simply returns. So what am I missing?