gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:42:34 26 4
gpt4 key购买 nike

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

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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com