C# 如何调试base.Dispose(disposing)样板上发生的NullReferenceException

C# 如何调试base.Dispose(disposing)样板上发生的NullReferenceException,c#,excel,vsto,C#,Excel,Vsto,我是c#和VSTO的新手 我有一个Excel外接程序,可以创建一个功能区和许多新按钮,这些按钮可以执行各种操作 我在加载项加载到Excel时,在Ribbon.Designer.cs文件的base.Dispose中收到一个NullReferenceException 根据文档所说,我可能没有使用new关键字就实例化了一些东西,但我不知道在哪里可以找到我没有做过的东西 今天早些时候我确实做了这项工作,但没有做任何备份,也没有连接到源代码管理,这在事后看来是个坏主意 在出现异常时,有很多空值,我不知道

我是c#和VSTO的新手

我有一个Excel外接程序,可以创建一个功能区和许多新按钮,这些按钮可以执行各种操作

我在加载项加载到Excel时,在Ribbon.Designer.cs文件的base.Dispose中收到一个NullReferenceException

根据文档所说,我可能没有使用new关键字就实例化了一些东西,但我不知道在哪里可以找到我没有做过的东西

今天早些时候我确实做了这项工作,但没有做任何备份,也没有连接到源代码管理,这在事后看来是个坏主意

在出现异常时,有很多空值,我不知道这是否有意义

基本上;我不知道我做了什么突然使这件事发生,也不知道如何调试它,在哪里设置我的中断,或者这一块样板文件如何融入到事情的计划中

当我没有包含绝大多数代码时,我并不期望有人能神奇地将bug清除出来,但是一些关于如何做的指导或解释将是非常棒的

异常时的本地窗口

  -           this  {ReportFramework.Ribbon}      ReportFramework.Ribbon
        -           base  {ReportFramework.Ribbon}      Microsoft.Office.Tools.Ribbon.RibbonBase {ReportFramework.Ribbon}
        +           base  {ReportFramework.Ribbon}      System.ComponentModel.Component {ReportFramework.Ribbon}
                    Base  null  Microsoft.Office.Tools.Ribbon.OfficeRibbon
        +           Context     '((Microsoft.Office.Tools.Ribbon.RibbonBase)(this)).Context' threw an exception of type 'System.NullReferenceException' object {System.NullReferenceException}
                    Factory     null  Microsoft.Office.Tools.Ribbon.RibbonFactory
        +           Global      '((Microsoft.Office.Tools.Ribbon.RibbonBase)(this)).Global' threw an exception of type 'System.NullReferenceException'  bool {System.NullReferenceException}
        +           Name  '((Microsoft.Office.Tools.Ribbon.RibbonBase)(this)).Name' threw an exception of type 'System.NullReferenceException'    string {System.NullReferenceException}
        +           OfficeMenu  '((Microsoft.Office.Tools.Ribbon.RibbonBase)(this)).OfficeMenu' threw an exception of type 'System.NullReferenceException'    Microsoft.Office.Tools.Ribbon.RibbonOfficeMenu {System.NullReferenceException}
        +           Parent      '((Microsoft.Office.Tools.Ribbon.RibbonBase)(this)).Parent' threw an exception of type 'System.NullReferenceException'  Microsoft.Office.Tools.Ribbon.RibbonComponent {System.NullReferenceException}
        +           RibbonId    '((Microsoft.Office.Tools.Ribbon.RibbonBase)(this)).RibbonId' threw an exception of type 'System.NullReferenceException'      string {System.NullReferenceException}
        +           RibbonType  '((Microsoft.Office.Tools.Ribbon.RibbonBase)(this)).RibbonType' threw an exception of type 'System.NullReferenceException'    string {System.NullReferenceException}
        +           RibbonUI    '((Microsoft.Office.Tools.Ribbon.RibbonBase)(this)).RibbonUI' threw an exception of type 'System.NullReferenceException'      Microsoft.Office.Core.IRibbonUI {System.NullReferenceException}
        +           StartFromScratch  '((Microsoft.Office.Tools.Ribbon.RibbonBase)(this)).StartFromScratch' threw an exception of type 'System.NullReferenceException'    bool {System.NullReferenceException}
        +           Tabs  '((Microsoft.Office.Tools.Ribbon.RibbonBase)(this)).Tabs' threw an exception of type 'System.NullReferenceException'    System.Collections.Generic.IList<Microsoft.Office.Tools.Ribbon.RibbonTab> {System.NullReferenceException}
        +           Tag   '((Microsoft.Office.Tools.Ribbon.RibbonBase)(this)).Tag' threw an exception of type 'System.NullReferenceException'     object {System.NullReferenceException}
        +           Non-Public members            
                    btnAddCETotals    null  Microsoft.Office.Tools.Ribbon.RibbonButton
                    btnLinkBank null  Microsoft.Office.Tools.Ribbon.RibbonButton
                    btnCEDataTransfer null  Microsoft.Office.Tools.Ribbon.RibbonButton
                    *...More btn's*
                    IsClose     false bool
                    LargeColWidth     0.0   double
        +           objCReport  {ReportFramework.Classes.CReport}   ReportFramework.Classes.CReport
                    objFormat   null  ReportFramework.Classes.CFormat
                    oExcel      null  Microsoft.Office.Interop.Excel.Application
                    PageWidthLandscape      0.0   double
                    PageWidthPortrait 0.0   double
                    SmallColWidth     0.0   double
                    tabBoyceTools     null  Microsoft.Office.Tools.Ribbon.RibbonTab
                    userName    null  string
                    WorksheetResult   null  Microsoft.Office.Interop.Excel.Worksheet
                    disposing   false bool
第30行是基础。处理下面的行

Ribbon.Designer.cs

namespace ReportFramework
  {
      partial class Ribbon : Microsoft.Office.Tools.Ribbon.RibbonBase
      {
          /// <summary>
          /// Required designer variable.
          /// </summary>
          private System.ComponentModel.IContainer components = null;

          public Ribbon()
              : base(Globals.Factory.GetRibbonFactory())
          {
              InitializeComponent();
          }

          /// <summary> 
          /// Clean up any resources being used.
          /// </summary>
          /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
          protected override void Dispose(bool disposing)
          {
              if (disposing && (components != null))
              {
                  components.Dispose();
              }

              base.Dispose(disposing);

          }

          #region Component Designer generated code

          /// <summary>
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// </summary>
          private void InitializeComponent()
          {
              this.tabBoyceTools = this.Factory.CreateRibbonTab();
              this.grpBoyceCAL = this.Factory.CreateRibbonGroup();
              this.btnGenerateCAL = this.Factory.CreateRibbonButton();
             *...Lots more code here; not sure if it's pertinent. can post if necessary.*

      }

      partial class ThisRibbonCollection
      {
          internal Ribbon Ribbon
          {
              get { return this.GetRibbon<Ribbon>(); }
          }
      }
  }
命名空间报告框架
{
分部类功能区:Microsoft.Office.Tools.Ribbon.RibbonBase
{
/// 
///必需的设计器变量。
/// 
private System.ComponentModel.IContainer components=null;
公共丝带()
:base(Globals.Factory.GetRibbonFactory())
{
初始化组件();
}
///  
///清理所有正在使用的资源。
/// 
///如果应释放托管资源,则为true;否则为false。
受保护的覆盖无效处置(布尔处置)
{
if(处理和(组件!=null))
{
组件。Dispose();
}
基地。处置(处置);
}
#区域组件设计器生成的代码
/// 
///设计器支持所需的方法-不修改
///此方法的内容与代码编辑器一起使用。
/// 
私有void InitializeComponent()
{
this.tabBoyceTools=this.Factory.CreateRibbonTab();
this.grpboyecal=this.Factory.CreateRibbonGroup();
this.btnGenerateCAL=this.Factory.CreateRibbonButton();
*…此处有更多代码;不确定是否相关。如有必要,可以发布*
}
部分类ThisRibbonCollection
{
内部带状
{
获取{返回this.GetRibbon();}
}
}
}

FWIW,在选中“项目设置”>“为程序集签名”中的复选框并尝试使用临时密钥签名后,我开始收到此异常


我确信这个错误可能是由许多其他错误引起的,但希望这能帮助一些人。

是否有可能调用Dispose两次?Google的“site:stackoverflow.com如何在异常时中断visual studio”。NullReferenceException表示发生此问题的行看起来像
a.b()
c=a.b
或..和a(代码的左侧)是
null
,它不应该被调用两次,jaket;这是可能的,但我找不到任何包含该短语的调用/代码,所以我将尝试xmojmr关于打破异常的建议。会让你知道我的进展。嗨xmojmr-你能把你的评论复制到答案中吗?启用“抛出”后立即找到它“调试>异常”下的“异常”,展开“公共运行时>系统”并在抛出的NullReference勾号旁边。将我带到导致问题的确切代码行,正如错误描述的那样,我看起来在不使用“新建”的情况下做了一些事情
namespace ReportFramework
  {
      partial class Ribbon : Microsoft.Office.Tools.Ribbon.RibbonBase
      {
          /// <summary>
          /// Required designer variable.
          /// </summary>
          private System.ComponentModel.IContainer components = null;

          public Ribbon()
              : base(Globals.Factory.GetRibbonFactory())
          {
              InitializeComponent();
          }

          /// <summary> 
          /// Clean up any resources being used.
          /// </summary>
          /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
          protected override void Dispose(bool disposing)
          {
              if (disposing && (components != null))
              {
                  components.Dispose();
              }

              base.Dispose(disposing);

          }

          #region Component Designer generated code

          /// <summary>
          /// Required method for Designer support - do not modify
          /// the contents of this method with the code editor.
          /// </summary>
          private void InitializeComponent()
          {
              this.tabBoyceTools = this.Factory.CreateRibbonTab();
              this.grpBoyceCAL = this.Factory.CreateRibbonGroup();
              this.btnGenerateCAL = this.Factory.CreateRibbonButton();
             *...Lots more code here; not sure if it's pertinent. can post if necessary.*

      }

      partial class ThisRibbonCollection
      {
          internal Ribbon Ribbon
          {
              get { return this.GetRibbon<Ribbon>(); }
          }
      }
  }