Click here to Skip to main content
15,883,901 members
Please Sign up or sign in to vote.
2.11/5 (2 votes)
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

C#
    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.

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)
Posted
Updated 16-Feb-15 5:10am
v2
Comments
StM0n 16-Feb-15 11:59am    
Not quite sure, but your call of <<MyWebService>> does not return anything... at least not in your submitted code.

Otherwise, could you debug your project?
Richard Deeming 16-Feb-15 16:10pm    
!(e.Error == null) would normally be written as: e.Error != null

It sounds like ProgressForm.ActiveForm is returning null. Try checking for null before trying to call a method on it.
Member 11166005 19-Feb-15 0:05am    
check this link i think it will help you
http://www.codeguru.com/columns/dotnet/working-with-background-workers-in-c.html

First check the image is reading correctly .If the image is reading properly
then you won't get System.NullReferenceException ..Keep a breakpoint in the project and check it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900