Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullReferenceException adding to a dictionary - very certain that nothing is null

Tags:

c#

dictionary

Where Sessions is a Dictionary<Guid, WebSession>, and NewSession is new WebSession() I have this line:

Sessions.Add(NewSession.SessionID, NewSession);

Now at this point you're probably rolling your eyeballs and thinking "well either Sessions is null, or NewSession.SessionID is null." However:

Sessions == null
false
NewSession.SessionID == null
false
NewSession == null
false

It's pretty intermittent. Happens maybe one time in 50. And whenever it happens, I can just do Sessions.Add(NewSession.SessionID, NewSession); in the immediate window and it works fine.

The constructor for WebSession is synchronous, and Sessions is a vanilla dictionary with no added sugar.

I'm pretty sure I've done due diligence at this point. It's a harmless enough thing to happen in my application and it's trapped and handled cleanly - but I'm stumped as to what causes it in the first place.

Edit: I'm wondering if it's because my WebSession inherits : Dictionary<String, Object> but its constructor doesn't call base() - that still wouldn't explain it though since I can check that the object isn't null before doing Add(..)

like image 425
PhonicUK Avatar asked Mar 03 '13 19:03

PhonicUK


People also ask

Why am I getting a NullReferenceException?

This infamous and dreaded error message happens when you get a NullReferenceException. This exception is thrown when you try to access a member—for instance, a method or a property—on a variable that currently holds a null reference.

How do I fix NullReferenceException in C#?

Solutions to fix the NullReferenceException To prevent the NullReferenceException exception, check whether the reference type parameters are null or not before accessing them. In the above example, if(cities == null) checks whether the cities object is null or not.

How do I fix this error system NullReferenceException object reference not set to an instance of an object?

The best way to avoid the "NullReferenceException: Object reference not set to an instance of an object” error is to check the values of all variables while coding. You can also use a simple if-else statement to check for null values, such as if (numbers!= null) to avoid this exception.

Can NullReferenceException be caught?

Find out which exception you're receiving, and either catch that specific exception, or else prevent it from occurring in the first place. You should never catch NullReferenceException.

What is a nullreferenceexception?

This infamous and dreaded error message happens when you get a NullReferenceException. This exception is thrown when you try to access a member—for instance, a method or a property—on a variable that currently holds a null reference. But what is a null reference? What are “references” in the first place?

What is a null reference in a class?

Classes are reference types. So, a reference is what a variable of a reference type contains. These variables can point to “nothing”, though, and that’s what we call a null reference: a reference that doesn’t point to any object. When you try to call a method or another member on the said variable, you got the NullReferenceException.

What is object null reference not set to an instance?

Object reference not set to an instance of an object Null reference errors are responsible for a good percentage of all application bugs. They are usually very simple problems caused by not adding additional logic to ensure that objects have valid values before using them.

How to prevent the nullreferenceexception in displaycities()?

If the caller of the DisplayCities () function pass a null IList value then it will raise a NullReferenceException. To prevent the NullReferenceException exception, check whether the reference type parameters are null or not before accessing them.


Video Answer


1 Answers

You need to use a thread-safe collection such as ConcurrentDictionary, or implement your own sychronization.

Attempting to access a Dictionary<TKey,TValue> from multiple threads can result in heisenbugs, which may well manifest themselves as a NullReferenceException.

like image 66
Joe Avatar answered Oct 18 '22 20:10

Joe