Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/314.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么创建注册表子项只对某些计算机返回NullReferenceException?_C#_.net_Registry_Nullreferenceexception_Code Access Security - Fatal编程技术网

C# 为什么创建注册表子项只对某些计算机返回NullReferenceException?

C# 为什么创建注册表子项只对某些计算机返回NullReferenceException?,c#,.net,registry,nullreferenceexception,code-access-security,C#,.net,Registry,Nullreferenceexception,Code Access Security,有一个.NET应用程序是用c sharp编写的,注册时需要与.asmx Web服务通信。许多用户报告了btnRegister click事件中的以下错误: NullReferenceException:对象引用未设置为对象的实例 .NET应用程序->3.5框架 Web服务器->ASP.NET 4.0集成 令我困惑的是,一些用户可以注册而没有问题,而其他用户则无法解决此错误。能够和不能解析WebService对象的人之间似乎没有公共链接。注册失败的唯一解决方案是选择网络上的另一台计算机来安装应用程

有一个.NET应用程序是用c sharp编写的,注册时需要与.asmx Web服务通信。许多用户报告了btnRegister click事件中的以下错误:

NullReferenceException:对象引用未设置为对象的实例

.NET应用程序->3.5框架 Web服务器->ASP.NET 4.0集成

令我困惑的是,一些用户可以注册而没有问题,而其他用户则无法解决此错误。能够和不能解析WebService对象的人之间似乎没有公共链接。注册失败的唯一解决方案是选择网络上的另一台计算机来安装应用程序。通常,如果另一台计算机可用,注册过程将按预期进行

是否有一些配置设置、错误处理或方法调用没有发生,可能导致这种看似随机的行为或我缺少的某种模式

由于我无法复制错误,因此除了显示btnRegister_Click事件引发的NullReferenceException错误的有限屏幕截图外,我没有异常的调用堆栈

好的,我已经尝试减少代码量,尽管这基本上都是非常基本的注册表编写,并且我正在显示通用方法名称和参数

显然,写入当前用户的注册表没有什么基本的内容,因为调用以下命令创建RegistrySubkey时,在调用WebService之前会抛出NullReferenceException错误:

我仍然无法在我拥有或有权访问的任何计算机上重现该错误,这导致我需要了解在何种情况下,RegistryKey在调用时会返回NullReferenceException(如上代码所示),以及什么策略/权限可以绕过任何限制

关联代码-根据Tom W建议重新分解尝试1次:

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());
     }    
}        

发布异常调用堆栈和关联代码在单个方法中有太多的代码,如果没有更多信息,几乎不可能猜测是什么引发了异常。如果您能够,我强烈建议重构此方法并实现一些有意义的异常处理和日志记录。至少堆栈跟踪是必要的。我想知道在什么条件下WebService对象将在c中返回nullReferenceException
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());
     }    
}