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(..)
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.
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.
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.
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.
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?
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.
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.
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.
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With