Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8869117
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:24:27+00:00 2026-06-14T17:24:27+00:00

I am facing NullPointerException in below code as it is happening very rarely and

  • 0

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;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:24:28+00:00Added an answer on June 14, 2026 at 5:24 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im facing with a very strange issue and i dont know yet the source
Im facing an issue which I believe to be VAO-dependant, but Im not sure..
I am trying to make an Enterprise Application but facing a very strange error.
Facing some issue in Talend Admin Console, the jobs that are running from IDE
issue facing while loading the messages in to mq channel, recieving an error like
Iam facing problem in understanding and converting a matlab code into opencv. I want
iam facing problem in passing array to view. this is my controller code.. function
we are facing a strange issue. we have a (custom)file upload control in our
Im facing a bit of an issue when trying to validate a decimal property
Facing a need for a single CMS we can use for rapid deployment of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.