每当单击树节点时,我都会收到此错误消息:
Picture-Resize.exe 中出现“System.NullReferenceException”类型的未处理异常附加信息:对象引用未设置为对象的实例。
我的代码如下:
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)
{
}
}
}
发生的情况是异常处理程序捕获它,但是由于抛出了异常,它需要用户单击两次才能使单击处理程序生效。
为什么我会收到此错误,我该如何避免?