none
System.NullReferenceException is being thrown in background worker C# RRS feed

  • Question

  • I have a windows form application which is written in managed c#. In my application on login click i have to validate user credentials with a remote server through web service invocation. When there is internet connection(dongle) available there is no problem, but when internet connection is made off while calling web service System.NullReferenceException: Object reference not set to an instance of an object is being thrown. This scenario is every time reproducible.
     
    I have used backgroungworker component from tool box and two eventhandler functions set from properties window of the same namely DoWork and RunWorkerCompleted.
     
    when my background worker is executing web service i show progress(this.showdialog()) bar in a domodal manner from separate windows form and dispose that progress bar at RunWorkerCompleted function.
     
    //Following is my code snippets:
     
    // in Form1_designer.cs
     


    BackgroundWorker bw = new BackgroundWorker();
    bw.WorkerSupportsCancellation = true;
    bw.DoWork += 
    new DoWorkEventHandler(bw_DoWork);
    bw.RunWorkerCompleted += 
    new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
     
    //In form1.cs
    private btnClick_login(...)
    {
    bw.RunWorkerAsync();
    }
    private void bw_DoWork(object sender, DoWorkEventArgs e)
    {
    BackgroundWorker worker = sender as BackgroundWorker;
    if ((worker.CancellationPending == true))
    {
    e.Cancel = true;
    }
    else
    {
    e.Result = MyWebservice();
    }
    }
    }
     
    void MyWebService()
    {
    ActualWebService();
    }
    private void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
    if ((e.Cancelled == true))
    {
    TerminateApplication();
    }
     
    else if (!(e.Error == null))
    {
    loggedToFile(e.Error.Message);
    }
     
    else
    {
    ProgressForm.ActiveForm.Close();
    }
    }
     
    Area of concern: While application is executing webiservice and internet dongle is removed then exception is being thrown by runtime though webservice call successfully is done with proper value of loss of internet connectivity
    Kindly let me know if i am doing something wrong that leads to this situation
    Thanks in advance.

    StackTrace:

    System.NullReferenceException: Object reference not set to an instance of an object.
       at LicenseManagerUI.NissanLicenseManagerForm.backgroundWorker1_RunWorkerCompleted(Object sender, RunWorkerCompletedEventArgs e)
       at System.ComponentModel.BackgroundWorker.OnRunWorkerCompleted(RunWorkerCompletedEventArgs e)
       at System.ComponentModel.BackgroundWorker.AsyncOperationCompleted(Object arg)

    Monday, February 16, 2015 4:01 PM

Answers

  • The callstack does not line up with the code you provided.  If you look at the callstack it is blowing up inside backgroundWorker1_RunWorkerCompleted. This looks like a method that was created when you used the Properties window and double clicked inside the RunWorkerCompleted event box.  The designer would have auto-created this method for you. 

    In your code you are explicitly creating a BWC called bw. Since you are programmatically configuring this code the designer wouldn't come into play here. I suspect that you have a BWC associated with the form via the designer and that is causing the problem, not the code you posted.  Open the form in the designer and see if there is a backgroundWorker1 component in the designer. If so, and you don't need it, then remove it along with the above mentioned method.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Wednesday, February 18, 2015 4:10 PM
    Moderator

All replies

  • Perhaps you could tell us which line of code throws the exception. The stack trace indicates it is somewhere in the RunWorkerCompleted method. In the cases where you get the exception, did the worker end normally, with an error, or because it was cancelled. That would tell you what path you took through the RunWorkerCompleted method. One of the objects you refer to in that method is null and that is causing the exception.
    Monday, February 16, 2015 4:12 PM
  • Since the first line in stack trace is in backgroundWorker1_RunWorkerCompleted() and not in any of the inner methods, I'm going to guess that ProgressForm.ActiveForm is null when the ProgressForm.ActiveForm.Close() is called. Better add a check for null at there.
    Tuesday, February 17, 2015 1:37 AM
    Answerer
  • The callstack does not line up with the code you provided.  If you look at the callstack it is blowing up inside backgroundWorker1_RunWorkerCompleted. This looks like a method that was created when you used the Properties window and double clicked inside the RunWorkerCompleted event box.  The designer would have auto-created this method for you. 

    In your code you are explicitly creating a BWC called bw. Since you are programmatically configuring this code the designer wouldn't come into play here. I suspect that you have a BWC associated with the form via the designer and that is causing the problem, not the code you posted.  Open the form in the designer and see if there is a backgroundWorker1 component in the designer. If so, and you don't need it, then remove it along with the above mentioned method.

    Michael Taylor
    http://blogs.msmvps.com/p3net

    Wednesday, February 18, 2015 4:10 PM
    Moderator