views:

18

answers:

2

I am having a treeview with one root node . I have written MouseHoverEvent as follows

   private void tvwACH_NodeMouseHover(object sender, TreeNodeMouseHoverEventArgs e)
    {

        string strFile = string.Empty;
        if (e.Node.Parent.Text == "FileHeader")
        {
            strFile = e.Node.ToString();

            string str = strFile.Substring(10);
            StringComparison compareType = StringComparison.InvariantCultureIgnoreCase;
            string fileName = Path.GetFileNameWithoutExtension(str);
            string extension = Path.GetExtension(str);
            if (extension.Equals(".txt", compareType))
            {

                StringBuilder osb = new StringBuilder();
                objFileHeader.getFileHeader(str, out osb);
                e.Node.ToolTipText = Convert.ToString(osb);
            }
        }

    }

But if i had my mouse on the root node i am getting an error as null exceptio handled. If i had my mouse hover the root node nothing should be happened. Can any one help me please.

A: 

Root Node does not have parent node set.So you should not refer e.Node.Parent.Text for Root node. You need to use conditional statement checking whether this node is root node or not.If current node is root you should properly handle it.You can handle the exceptions as well to resolve this problem.

Sandeep
+1  A: 
private void tvwACH_NodeMouseHover(object sender, TreeNodeMouseHoverEventArgs e)
{
     string strFile = string.Empty;

     // the problem is here, root node does not have a parent
     // also added a fix
     if (e.Node.Parent != null && e.Node.Parent.Text == "FileHeader")
     {
          strFile = e.Node.ToString();

          string str = strFile.Substring(10);
          StringComparison compareType = StringComparison.InvariantCultureIgnoreCase;
          string fileName = Path.GetFileNameWithoutExtension(str);
          string extension = Path.GetExtension(str);
          if (extension.Equals(".txt", compareType))
          {
              StringBuilder osb = new StringBuilder();
              objFileHeader.getFileHeader(str, out osb);
              e.Node.ToolTipText = Convert.ToString(osb);
          }
     }
}
devnull