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?
According to the comments on your question, the
Tagproperty of a tree node isnull.You are assigning a non-
nullvalue to every tree node in youraddToParentNodemethod, but somewhere, there must be a start and you must be creating a root node. Therefore, that root node apparently has itsTagproperty still set tonull.