5

I am facing NullPointerException in below code as it is happening very rarely and I tried to debug to replicate the issue but no luck. Can anybody help me what can cause NullPointerException here.

private static void MyTaskCompletedCallback(IAsyncResult res)
    {
        var worker = (AsyncErrorDelegate)((AsyncResult)res).AsyncDelegate;
        var async = (AsyncOperation)res.AsyncState;
        worker.EndInvoke(res);
        lock (IsAsyncOpOccuring)
        {
            IsBusy = false;
        }
        var completedArgs = new AsyncCompletedEventArgs(null, false, null);
        async.PostOperationCompleted(e => OnTaskCompleted((AsyncCompletedEventArgs)e), completedArgs);
    }

Null Pointer exception is reported at var async = (AsyncOperation)res.AsyncState;

Code from where I am invoking it

 var context = HttpContext.Current;
        AsyncErrorDelegate bkWorker = SendErrorMail;
        AsyncCallback completedCallback = MyTaskCompletedCallback;
        lock (IsAsyncOpOccuring)
        {
            if (IsBusy)
            {
                //Do we need to do something if repeated async getting call in case of error occuring at same time by different users.

            }

            AsyncOperation async = AsyncOperationManager.CreateOperation(null);
            bkWorker.BeginInvoke(context,completedCallback, async);
            IsBusy = true;
        }
3
  • 5
    do you actually mean a "null pointer" exception? or is it a NullReferenceException ? pointer != reference (well, not strictly) Commented Nov 21, 2012 at 10:58
  • K; now that you've edited that: how do you do the BeginInvoke ? is there any chance you're simply passing in a null state ? Also: for info, the handling of IsBusy is dangerous in two different ways: if EndInvoke reports an exception, it'll never be marked as not busy, and you can't have more than one async task without corrupting that. Personally, I'd use an interlocked counter, but that might just be me. Commented Nov 21, 2012 at 11:05
  • I have updated the above code for the begin invoke.You rightly pointed IsBusy can be dangerous...will update it Commented Nov 21, 2012 at 11:09

2 Answers 2

3

Null Pointer exception is reported at var async = (AsyncOperation)res.AsyncState;

We can logically deduce that this is not actually the case.

If the line before worked, we know that res is not null. AsyncState is object, so no custom operators are involved here, which means the cast is thus a type-check - which can either return null (without erroring), or can raise an invalid-cast exception.

If you are seeing a NullReferenceException, that leaves 2 options:

  • res is null and it is the line above that is erroring (which: we shouldn't actually expect - that will not happen)
  • the error is actually coming from EndInvoke, the line after

(the exact line often gets slightly confused when an exception is involved).

I suggest you add logging between each, to track what is happening. I also suggest you explicitly try around the EndInvoke, since that can throw exceptions (it re-throws any exception from the async operation).

In the more general case, a third option would have been:

  • AsyncOperation is a struct, and AsyncState is null

However, in this case we can rule that out by deduction, because if AsyncOperation were a struct, the following would never box to null (only an empty AsyncOperation? would box to null):

AsyncOperation async = AsyncOperationManager.CreateOperation(null);
bkWorker.BeginInvoke(context,completedCallback, async);
Sign up to request clarification or add additional context in comments.

Comments

1

Should

var async = (AsyncOperation)asyncResult.AsyncState;

not be

var async = (AsyncOperation)res.AsyncState;

?

3 Comments

That would be a compiler error. I guess the sample code is just inconsistent.
@usr not if asyncResult is a static field
@MarcGravell although turned out not to be true, that was a good idea.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.