This is a migrated thread and some comments may be shown as answers.

null referance Exception with Treeveiw

3 Answers 84 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Gajanan
Top achievements
Rank 2
Gajanan asked on 11 May 2010, 10:12 AM
hi All

in my page i am using treeview and tabstrip control.in treeview employee is displaying in child node and branch is displaying in parent Node. when i am selecting child node every thing is working properly but while selecting parent node then it's throwing nullrefrence Exception.
and one more thing i am using Telerik latest control i.e. 2010.
    previously it was working with 2009. but now it's throwing Exception
so how to get parent Node is seleted or not in Treeview.

my code Is:-

 protected void treeOppGroups_NodeClick(object o, RadTreeNodeEventArgs e)
    {
        //if (treeOppGroups.SelectedNode.ParentNode.Selected)
       if(e.Node.ParentNode.Selected)
        {
            DoReadOnly();
        }
        else
        {
            DoReadOnlyFalse();
       }
  }


Please Help Meeeeeeeeeeeeeeeeee......

3 Answers, 1 is accepted

Sort by
0
Accepted
Nikolay Tsenkov
Telerik team
answered on 11 May 2010, 11:52 AM
Hello Gajanan S,

The problem with the code is that you are assuming that every clicked node has a parent node and that is not true by definition. The root nodes doesn't have ParentNode-s.

You have to first check for the existence of the ParentNode and then to see whether it's selected or not.
See the example below:
if (e.Node.ParentNode != null)
{
    if (e.Node.ParentNode.Selected)
    {
        DoReadOnly();
    }
    else
    {
        DoReadOnlyFalse();
    }
}

Hope this will help you!


Regards,
Nikolay Tsenkov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0
Gajanan
Top achievements
Rank 2
answered on 11 May 2010, 12:34 PM
hi,
i used your code
even i select Parent node in treeview then also it's not executing my first IF block i.e
if (e.Node.ParentNode.Selected)
    {
        DoReadOnly();
    }

that
in my code i am checking if it's child node i have to enabled all the controls on that tab otherwise those are disabled.that's why i am checking for Parent Node.
0
Nikolay Tsenkov
Telerik team
answered on 11 May 2010, 01:12 PM
Hello Gajanan S,

Well, you get the null-reference exception without the additional check. And this is when the node doesn't have a parent, right? Then when we check for it, this case will logically not work, because the element doesn't have parent node. I don't see the problem here.

Maybe the thing you want to achieve is to check if this node is terminal (doesn't have any children)? If that so than the code will state:
if (e.Node.Nodes.Count != 0){
 
    DoReadOnly();
 
} else {
 
    DoReadOnlyFalse();
 
}
That way you will check for existence of children for the clicked node. If it has - then the mode should be read only, right?

Hope this solves your problem!


Regards,
Nikolay Tsenkov
the Telerik team

Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Tags
TreeView
Asked by
Gajanan
Top achievements
Rank 2
Answers by
Nikolay Tsenkov
Telerik team
Gajanan
Top achievements
Rank 2
Share this question
or