My TreeView control is displaying the structure of some selected hard disk drive. In my addToParentNode, I make a call from after the tree view is expanded. But when I pass the node from one method to another, an "Object reference not set to an instance of an object" exception is thrown.
void addToParentNode(TreeNode childNodes)
{
DirectoryInfo getDir = new DirectoryInfo(childNodes.Tag.ToString());
DirectoryInfo[] dirList = getDir.GetDirectories();
foreach (DirectoryInfo dir in dirList)
{
TreeNode parentNode = new TreeNode();
parentNode.Text = dir.Name;
parentNode.Tag = dir.FullName;
childNodes.Nodes.Add(parentNode);
}
}
private void tv_fileExplore_AfterExpand(object sender, TreeViewEventArgs e)
{
foreach (TreeNode item in e.Node.Nodes)
{
addToParentNode(item);
}
}
Can someone point me in the right direction?
nullat the time your application crashes. Also, acatch (Exception)is rather unwise; if there's an exception, that's because something is wrong and you shouldn't just generally swallow (and thereby hide) all exceptions that might occur.null. I see you're setting theTagproperty to some non-nullvalue in youraddToParentNodemethod, but somewhere, you must be creating a root node. Are you sure you're settingTagfor that node, too?