I'm investigating an exception that a colleague just got while running an application through Visual Studio 2010:
System.NullReferenceException was unhandled by user code Message=Object reference not set to an instance of an object. Source=mscorlib StackTrace: at System.Collections.Generic.GenericEqualityComparer`1.Equals(T x, T y) at System.Collections.Concurrent.ConcurrentDictionary`2.TryGetValue(TKey key, TValue& value) at xxxxxxx.xxxxxxx.xxxxxxx.RepositoryBase`2.GetFromCache(TIdentity id)
Using .NET Reflector, I have looked at the code for GenericEqualityComparer<T>.Equals(T x, T y)
, and I can't see any possible cause for a NullReferenceException
.
//GenericEqualityComparer<T>.Equals(T x, T y) from mscorlib 4.0.30319.269 public override bool Equals(T x, T y) { if (x != null) { return ((y != null) && x.Equals(y)); } if (y != null) { return false; } return true; }
The type of T,
TKey
and TIdentity
are all the same type in this stack trace.
The type is a custom type called Identity
that implements IEquatable<Identity>
. It is immutable and cannot be constructed with null values for the fields that it uses in its implementation of Equals(Identity other)
. It also overrides Equals(object obj)
like this:
public override bool Equals(object obj) { if ((object)this == obj) { return true; } return Equals(obj as Identity); } public bool Equals(Identity other) { if ((object)this == (object)other) { return true; } if ((object)other == null) { return false; } if (!FieldA.Equals(other.FieldA)) { return false; } return FieldB.Equals(other.FieldB); }
I have a fairly exhaustive set of unit tests around the Equals
implementations. So, it will happily accept a value of null for other/obj and return false as expected.
The type does not either override the ==
operators nor !=
operators.
Even so, I would expect to see my class on top of the stack trace if the exception was being thrown from the implementation of Equals(Identity other)
in my Identity
class, but it says the NullReferenceException
is coming from mscorlib
.
I'm running on .NET Framework version 4.0.30319.269.
I don't have a memory dump, and I have not seen this before and have not reproduced it since. Still, I'm obliged to investigate and to be absolutely certain that it is not being caused by our code and that it won't happen in production.
So, the real question is: What caused this exception?
* Updates in response to Jordão *
Is it possible to call the method with an object that is not an Identity?
The ConcurrentDictionary<TKey, TValue>
is typed such that TKey
= Identity
and nothing subclasses Identity
. So, I can't see how it could be possible.
Is it possible to call the method with null?
Unit tests cover the scenario of calling all of the Equals
implementations with null.
What version of the code is the stack trace from? Maybe some older version susceptible to the exception?
I'm analyzing the same code that generated the exception. I have checked that the version of the .NET Framework running on my colleagues computer is also 4.0.30319.269.
Any multithreaded scenario could cause the exception? These are usually hard to reproduce, but might be worth investigating.
Yes, the code is multi-threaded and intended to be. So, that is why I'm using a ConcurrentDictionary
.
* Followup related to response from Jalal Aldeen Saa'd *
I would have thought that a race condition where some other thread sets x
to null
could only be the cause if the parameter x
was passed by reference using the 'ref' keyword. I set out to validate that theory with the following code:
ManualResetEvent TestForNull = new ManualResetEvent(false); ManualResetEvent SetToNull = new ManualResetEvent(false); [TestMethod] public void Test() { var x = new object(); var y = new object(); var t = Task.Factory.StartNew(() => { return Equals(x, y); }); TestForNull.WaitOne(); //wait until x has been tested for null value x = null; SetToNull.Set(); //signal that x has now been set to null var result = t.Result; Assert.IsFalse(result); } public bool Equals<T>(T x, T y) { if (x != null) { TestForNull.Set(); //signal that we have determined that x was not null SetToNull.WaitOne(); //wait for original x value to be set to null //would fail here if setting the outer scope x to null affected //the value of x in this scope return ((y != null) && x.Equals(y)); } if (y != null) { return false; } return true; }
and the test completes without errors.
I can force that behavior if I change the signature to pass x
and y
by reference (that is, public bool Equals<T>(ref T x, ref T y) then the test fails with a
NullReferenceException, but this does not match the method signature of
GenericEqualityComparer.Equals(T x, T y)`.
You can eliminate the exception by declaring the number of elements in the array before initializing it, as the following example does. For more information on declaring and initializing arrays, see Arrays and Arrays. You get a null return value from a method, and then call a method on the returned type.
This error is caused when an object is trying to be used by a script but does not refer to an instance of an object. To fix this example we can acquire a reference to an instance of the script using GameObject.
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.
The Null Reference Exception is not a major error, but one of the common ones and one of the basic and simple way to avoid the Null Reference Exception is to check the variable or property before moving ahead and accessing it. And a very basic way to do this is to check the variable within an if statement.
I'll lay out my hypothesis here.
The stack is leading you to believe that this is where the crash occurs, but it occurs elsewhere. We're looking at the wrong thread.
I don't know if this would be practical, but sometimes good old "printf debugging" helps. What if you print out the value you're looking for before calling TryGetValue
? You would see whether you strike a null or not.
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