I have the following function to remove specific nodes from a tree view control
private void PruneTree(TreeNode tn)
{
foreach (TreeNode item in tn.Nodes)
{
PruneTree(item);
}
if (tn.BackColor == Color.LightCoral && tn.Nodes.Count == 0)
{
tn.Remove();
}
}
I call it on a treeview root node, but it throws NullReferenceException
for the tn
at tn.Nodes
.
I don't expect a null item because I iterate the child nodes of a node and certainly they can't be null.
What could be the problem, I think as I remove some nodes from the tree, it affects the function.