Adding an item to the dictionary throws a NullReferenceException

The following code has only thrown a NullReferenceException a few times in the last few months, but I'm not quite sure why. The code is not mine, but it looks pretty straight 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 instance 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 is used as a dictionary key, but it seems impossible .

The code that calls it is simple:

 protected override void OnLoad(EventArgs e) { base.OnLoad(e); _mypage = GetPage(); } 

I also thought that the error could be with _section.Pages , but the section is never null and never sets anything. If .Pages[page] returns null, the MyPage constructor simply returns. So what am I missing?

+4
source share
6 answers

I had this problem and it turned out to be thread related.

By placing a synchronization lock around the line that is inserted into the dictionary, you can protect against errors:

This should be added as a class level definition:

 private static object _sync = new object(); 

This is the insert statement, surrounded by a synchronization lock:

 lock (_sync) { return _pages[pageType] = new MyPage(_section.Pages[page]); } 

This is the message that solved it for me: Throw a NullReferenceException when calling the set_item method of the Dictionary object in a multi-threaded script

+2
source

I think this is an exception because the dictionary does not have an element for the pageType key, and accessor returns null.

Try

 MyPage newPage = new MyPage(_section.Pages[page]); _pages.Add(pageType, newPage); return newPage; 

or if you are trying to reuse a record if it exists:

 MyPage newPage = new MyPage(_section.Pages[page]); if (_pages.ContainsKey(pageType)) _pages[pageType] = newPage; else _pages.Add(pageType, newPage); 
+1
source

Perhaps the dictionary user defines somewhere else a custom IEqualityComparer that does not work under some circumstances. Check where the code is creating the dictionary to see if the user resolver passed. Maybe the corrector may be null, but this means that this code never runs ...

+1
source

I would separate them for clarity, and then see what happens.

 var section = _section.Pages[page]; var newPage = new MyPage(section); _pages.Add(pageType,newPage); return newPage; 
0
source

I think either _pages or the pageType object is null. Another chance is the _section object.

0
source

Since TryGetValue failed, it looks called by _section.Pages [page]. Since the exception comes from the Dictionary.Insert method, it is most likely caused by the implementation of the equality / hash code for the page. Are there special IEqualityComparer or Equals / GetHashCode methods for the page type? How are these dictionaries created?

0
source

Source: https://habr.com/ru/post/1345573/


All Articles