0

There is a .NET application written in c sharp which requires communication with a .asmx WebService upon registration. A number of users have reported on the btnRegister click event the following error:

NullReferenceException: Object reference not set to an instance of an object.

.NET app --> 3.5 Framework Web Server --> ASP.NET 4.0 Integrated

The baffling thing to me is that some users can register without a problem while others are unable to resolve this error. There appear to be no common links between those who can and can not resolve the WebService object. The only solution to a failed registration has been to select another computer on the network to install the application. Usually if another computer is available the registration process will behave as expected.

Is there some configuration setting, error handling or method call that is not happening that could result in this seemingly random behavior or some pattern I'm missing?

Since I am unable to replicate the error I do not have the call stack for the exception other than the limited screen shots which show the NullReferenceException error thrown on the btnRegister_Click event.

Ok, I have attempted to reduce the amount of code (even though it's all basically pretty basic Registry writing and I'm displaying generic method names and parameters.

Apparently there is nothing basic about writing to the registry of the Current User as the NullReferenceException error is thrown prior to calling the WebService when the following is called to create RegistrySubkey:

var createKey = RegistryKey.CurrentUser.Create("Software\\{Company}\\{Product}");
createKey.SetValue("KeyName1", text1);
createKey.SetValue("KeyName2", text2);
createKey.Close();

I am still unable to reproduce the error on any machine I have or have access to which leads into my need to understand under what circumstances will RegistryKey return NullReferenceException when called as it is in the code above and what policy/permissions will workaround any limitations?

Associated code - ("Re-factoring" attempt 1 per Tom W advice):

private void btnRegister_Click(object sender, EventArgs e)   
{          
     try  
     {     
         WebService.Service1 dws = new Service1();
         Cursor.Current = Cursors.WaitCursor;
         string staString3 = "";
         if (btnRegister.Text == "Register")
         {
             if (textbox1.Text == "")
             {
                 Cursor.Current = Cursors.Default;
                 MessageBox.Show("Textbox1 is a required field.  Please enter a valid value for Textbox1");
                 textbox1.Focus();
                 return;
             }
             else if (textbox2.Text == "")
             {
                 Cursor.Current = Cursors.Default;
                 MessageBox.Show("Textbox2 is a required field.  Please enter a valid value for Textbox2");
                 textbox2.Focus();
                 return;
             }
             else
             {
                 initRegKey(textbox1.Text, textbox2.Text);
                 if (dws.Method1(textbox1.Text, staString3, textbox2.Text))
                 {
                     Cursor.Current = Cursors.Default;
                     if (MessageBox.Show("You have successfully registered your copy of {Product}." + Environment.NewLine, "Registration {Product}", MessageBoxButtons.YesNo) == DialogResult.Yes)
                     {
                         dws.Method3(textbox1.Text, staString3, textbox2.Text);
                         MessageBox.Show("Activation Code Sent!");
                     }
                     txtKey.Visible = true;
                     label4.Visible = true;
                     txtKey.Focus();
                     btnRegister.Text = "Activate";
                 }
                 else
                 {    
                    string OV = "";
                    string OB = "";
                    string[] installInfo = { OV, OB, Environment.OSVersion.VersionString, Environment.MachineName };                                
                    if (dws.Method2(textbox1.Text, staString3, textbox2.Text, installInfo))
                     {
                         txtKey.Focus();
                         Cursor.Current = Cursors.Default;
                         MessageBox.Show("You have successfully registered your copy of {Product}." + Environment.NewLine + "Your activation key will arrive in your e-mail's Inbox or Spam Folder shortly.");
                         txtKey.Visible = true;
                         label4.Visible = true;
                         txtKey.Focus();
                         btnRegister.Text = "Activate";
                     }
                     else if (dws.Method4(textbox1.Text, textbox2.Text) > 0)
                     {
                         this.Hide();
                         GlobalsVar.errornum = 3;
                         ErrorMessage frm = new ErrorMessage();
                         if (frm.ShowDialog(NativeWindow.FromHandle(Handle)) == DialogResult.Cancel)
                         {
                             IntPtr ip = new IntPtr(Globals.ThisAddIn.Application.Hwnd);
                             Show(NativeWindow.FromHandle(ip));
                         }
                     }
                     else
                     {
                         Hide();
                         GlobalsVar.errornum = 1;
                         ErrorMessage frm = new ErrorMessage();
                         if (frm.ShowDialog(NativeWindow.FromHandle(Handle)) == DialogResult.Cancel)
                         {
                             IntPtr ip = new IntPtr(Globals.ThisAddIn.Application.Hwnd);
                             Show(NativeWindow.FromHandle(ip));
                         }
                     }
                 }        
             }                        
         }    
         else
         {    
             textbox1.Enabled = !dws.Method5(textbox1.Text, staString3, txtKey.Text);
             textbox2.Enabled = !dws.Method5(textbox1.Text, staString3, txtKey.Text);
             txtKey.Enabled = !dws.Method5(textbox1.Text, staString3, txtKey.Text);
             if (dws.Method5(textbox1.Text, staString3, txtKey.Text))
             {
                 if (writeRegKeys(txtKey.Text, txtOrderID.Text, txtEmail.Text, GetProcIDMan(), 1, 1, 1, 1, 2, 1, DateTime.Today.Date, AssemblyVersion, 1, "http://{Company Name}/{Product}/"))
                 {
                     this.Close();
                 }
                 else
                 {
                 Hide();
                 GlobalsVar.errornum = 4;
                 ErrorMessage frm = new ErrorMessage();
                     if (frm.ShowDialog(NativeWindow.FromHandle(Handle)) == DialogResult.Cancel)
                     {
                         IntPtr ip = new IntPtr(Globals.ThisAddIn.Application.Hwnd);
                         Show(NativeWindow.FromHandle(ip));
                     } 
                 }                     
             }
             else
             {
                 Hide();
                 GlobalsVar.errornum = 2;
                 ErrorMessage frm = new ErrorMessage();
                 if (frm.ShowDialog(NativeWindow.FromHandle(Handle)) == DialogResult.Cancel)
                 {
                     IntPtr ip = new IntPtr(Globals.ThisAddIn.Application.Hwnd);
                     Show(NativeWindow.FromHandle(ip));
                 }                                                
             }
             ThisAddIn.Method0();
         }    
     }   
     catch (Exception ex)
     {    
          MessageBox.Show(ex.ToString());
     }    
}        
Daniel
  • 174
  • 5
  • Post the exception call stack and assosiated code – Tamir Vered Feb 09 '14 at 11:08
  • 1
    There is so much code in that single method it's going to be next to impossible to guess what is throwing that exception without more information. If you are able to, I strongly recommend refactoring this method and implementing some meaningful exception handling and logging. At the very least a stack trace is essential. – Tom W Feb 09 '14 at 12:52
  • I would like to know under what conditions WebService objects will return nullReferenceException in c# – Daniel Feb 09 '14 at 16:20

0 Answers0