0

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?

5
  • Please use the debugger to find out which variable or member is null at the time your application crashes. Also, a catch (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. Commented Aug 26, 2012 at 12:20
  • So the DirectoryInfo getDir = new DirectoryInfo(childNodes.Tag.ToString()); the childNodes.Tag comes through as null. How should I grab that information? Commented Aug 26, 2012 at 12:41
  • Ok, so you know what's null. I see you're setting the Tag property to some non-null value in your addToParentNode method, but somewhere, you must be creating a root node. Are you sure you're setting Tag for that node, too? Commented Aug 26, 2012 at 12:56
  • you solved my problem!!! thanks a million. I fat fingered my root node and typed Text instead of tag. That is incredible that you picked up on that without seeing the whole program. Thanks so much Commented Aug 26, 2012 at 13:16
  • Can you make an answer so I can accept it as the solution. Commented Aug 26, 2012 at 13:16

2 Answers 2

3

According to the comments on your question, the Tag property of a tree node is null.

You are assigning a non-null value to every tree node in your addToParentNode method, but somewhere, there must be a start and you must be creating a root node. Therefore, that root node apparently has its Tag property still set to null.

Sign up to request clarification or add additional context in comments.

Comments

0

There is not enough context but you might add some safeguards, and handle the exceptions (only catch them if you want to handle them. You could for example add a tooltip to the TreeNode to inform the user what is wrong for this node,

 void addToParentNode(TreeNode childNodes) 
    { 
        if ((childNodes != null) && (childNodes.Tag != null))
        {
            DirectoryInfo getDir = null;
            try {
               getDir = new DirectoryInfo(childNodes.Tag.ToString()); 
            }
            catch(SecurityException) {
                 childNodes.ToolTipText = "no access";
            }
            catch(PathTooLongException) {
                childNodes.ToolTipText = "path more then 254 chars";
            }
            catch(ArgumentException)
            {
               childNodes.ToolTipText = "huh?";
            }
            if (getDir!=null) && (!getDir.Exists) return;
            DirectoryInfo[] dirList = null;
            try {
               dirList = getDir.GetDirectories(); 
            }
            catch(UnauthorizedException) {
                childNodes.ToolTipText = "no access";
            }
            catch(SecurityException)
            {
                 childNodes.ToolTipText = "no access";
            } 
            if (dirList == null) return;
            foreach (DirectoryInfo dir in dirList) 
            { 
                TreeNode parentNode = new TreeNode(); 
                parentNode.Text = dir.Name; 
                parentNode.Tag = dir.FullName; 
                childNodes.Nodes.Add(parentNode); 
            } 
       }
    } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.