For the following piece of code (.NET v4.0.30319) I am getting a null reference exception indicated below in the second continuation.
Most interestingly this issue has only occurred in machines with 8GB RAM but other users have 16GB and more and they haven't reported any issue, and it is a very intermittent issue which leads me to suspect a garbage collection issue.
The GetData()
can be called multiple times so the first continuation of _businessObjectTask will only be called once as _businessObjects will have been populated from that point forward.
I imagine the Object reference not set to an instance of an object
exception is being thrown because either
_businessObjectTask
is null and it can't continue from a null task.items
variable which is passed in as a parameter is null somehow
The line number in my log file (748) points to the one highlighted below and not the lambda expression which points to #1 above instead of #2. I've played around in Visual Studio and each of the lines following businessObjectTask.ContinueWith()
are considered different i.e. if it was a null reference within the lambda expression it would give a different line number to 748
Any help would be greatly appreciated.
Edit: This isn't related to What is a NullReferenceException, and how do I fix it? because that is a much more basic explanation of null reference whereas this is much more complicated and subtle.
Exception
Full details of the stack trace (edited for simplicity with dummy class and namespace names)
Object reference not set to an instance of an object.
at ApplicationNamespace.ClassName`1.<>c__DisplayClass4e.<GetData>b__44(Task`1 t) in e:\ClassName.cs:line 748
at System.Threading.Tasks.ContinuationTaskFromResultTask`1.InnerInvoke()
at System.Threading.Tasks.Task.Execute()
Code
private static IDictionary<string, IBusinessObject> _businessObjects;
private Task<IDictionary<string, IBusinessObject>> _businessObjectTask;
public Task GetData(IList<TBusinessItem> items))
{
Log.Info("Starting GetData()");
if (_businessObjects == null)
{
var businessObjectService = ServiceLocator.Current.GetInstance<IBusinessObjectService>();
_businessObjectTask = businessObjectService.GetData(CancellationToken.None)
.ContinueWith
(
t =>
{
_businessObjects = t.Result.ToDictionary(e => e.ItemId);
return _businessObjects;
},
CancellationToken.None,
TaskContinuationOptions.OnlyOnRanToCompletion,
TaskScheduler.Current
);
}
var taskSetLEData = _businessObjectTask.ContinueWith // Line 748 in my code - "Object reference not set to an instance of an object." thrown here
(
task =>
{
items.ToList().ForEach
(
item =>
{
IBusinessObject businessObject;
_businessObjects.TryGetValue(item.Id, out businessObject);
item.BusinessObject = businessObject;
}
);
},
CancellationToken.None,
TaskContinuationOptions.OnlyOnRanToCompletion,
TaskScheduler.Default
);
}
Resolution:
So having used what I've learned from this question I went back to the original code and figured it all out.
Turns out the reason for this NRE was because the _businessObjectTask
is non-static where as the _businessObjects
is static.
This means that _businessObjects
is null the first time GetData()
is called which then sets _businessObjectTask
to non-null. Then when _businessObjectTask.ContinueWith
gets called it is non-null and continues without problem.
However if a second instance of this class above is instantiated, _businessObjects
has already been populated so _businessObjectTask
remains null. Then when _businessObjectTask.ContinueWith
gets called, a NRE on _businessObjectTask
is thrown.
There were a couple of options I could have taken but I ended up removing the _businessObjectTask
to a synchronous method call which means that I dont need to use the continuation anymore and I set _businessObjects
once.