c# - 单击 TreeNode 时发生类型为 'System.NullReferenceException' 的未处理异常

标签 c# winforms treeview

每当单击树节点时,我都会收到此错误消息:

An unhandled exception of type 'System.NullReferenceException' occurred in Picture-Resize.exe Additional information: Object reference not set to an instance of an object.

我的代码如下:

public Form1()
{
    InitializeComponent();
    this.treeView1.NodeMouseClick += new TreeNodeMouseClickEventHandler (this.treeView1_NodeMouseClick);
    PopulateTreeView();
}

private void PopulateTreeView()
{
   TreeNode rootNode;

   DirectoryInfo info = new DirectoryInfo(@"c:\\");
   if (info.Exists)
   {
       rootNode = new TreeNode(info.Name);
       rootNode.Tag = info;
       GetDirectories(info.GetDirectories(), rootNode);
       treeView1.Nodes.Add(rootNode);
   }
}

private void GetDirectories(DirectoryInfo[] subDirs, TreeNode nodeToAddTo)
{
   TreeNode aNode;
   DirectoryInfo[] subSubDirs;
   foreach (DirectoryInfo subDir in subDirs)
   {
       aNode = new TreeNode(subDir.Name, 0, 0);
       aNode.Tag = subDir;
       aNode.ImageKey = "folder";
       try
       {
           /*  subSubDirs = subDir.GetDirectories();
             if (subSubDirs.Length != 0)
             {
                 GetDirectories2(subSubDirs, aNode);
             }*/
       }
       catch (System.UnauthorizedAccessException)
       {
           subSubDirs = new DirectoryInfo[0];
       }
       nodeToAddTo.Nodes.Add(aNode);
   }
}


void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{               
   //if (treeView1.SelectedNode.Nodes.Count > 0) { MessageBox.Show("Child node exists"); } else { MessageBox.Show("Child Node does not exist"); }

   try
   {
       TreeNode newSelected = e.Node;
     //  treeView1.SelectedNode.Nodes.Add("test");

       listView1.Items.Clear();
       DirectoryInfo nodeDirInfo = (DirectoryInfo)newSelected.Tag;
       ListViewItem.ListViewSubItem[] subItems;

       ListViewItem item = null;

       foreach (DirectoryInfo dir in nodeDirInfo.GetDirectories())
       {
           item = new ListViewItem(dir.Name, 0);
           subItems = new ListViewItem.ListViewSubItem[]
            {new ListViewItem.ListViewSubItem(item, "Directory"), 
             new ListViewItem.ListViewSubItem(item, 
                dir.LastAccessTime.ToShortDateString())};
           item.SubItems.AddRange(subItems);
           listView1.Items.Add(item);
       }
       foreach (FileInfo file in nodeDirInfo.GetFiles())
       {
           item = new ListViewItem(file.Name, 1);
           subItems = new ListViewItem.ListViewSubItem[]
            { new ListViewItem.ListViewSubItem(item, "File"), 
             new ListViewItem.ListViewSubItem(item, 
                file.LastAccessTime.ToShortDateString())};

           item.SubItems.AddRange(subItems);
           listView1.Items.Add(item);
       }

       listView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize);
   }
   catch (Exception ex)
   {
        if (ex is System.NullReferenceException || ex is System.UnauthorizedAccessException)
        {
        }
   }
}

发生的是异常处理程序捕获它,但由于抛出异常,因此需要用户单击两次以使单击处理程序生效。

为什么会出现此错误,如何避免?

最佳答案

您需要调试您的代码。

我假设您使用的是 Visual Studio,如果是这样,请执行以下操作:

  1. 转到“调试”菜单。
  2. 单击“异常(exception)...”选项。
  3. 应出现以下对话框: enter image description here

请注意,Common Language Runtime Exceptions 复选框已选中。

单击“确定”后,现在只要您的代码或 .NET Framework 抛出异常,当您调试代码时,调试器将在抛出异常的行处停止。这使得查找“损坏”的位置变得更加容易。

关于c# - 单击 TreeNode 时发生类型为 'System.NullReferenceException' 的未处理异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17251311/

相关文章:

c# - 如何将 TreeView 复选框设置为部分选中?

c# - Web API Controller 不适用于 ASP.NET Web 窗体中的 POST/PUT/DELETE

c# - 如何设置将 ImageList 中的图像转换为图标类型?

.net - Windows重启后如何自动启动自己的可执行文件?

c# - 如何使用设计人员友好的预绑定(bind)数据源创建派生组合框?

javascript - 使用 angular.treeview 和 Angular ui-sortable 将子节点拖放到父节点

c# - Windows 窗体的基本布局

c# - 如果字典键为可为空的十进制,则 Mvc SelectList 不绑定(bind)

c# - 我的自定义InputStream 和outputStream 对象可以在C# HTTP 程序中工作吗?

c# - 为什么这个 C# 代码这么慢?优化目录/文件搜索和添加所需