Click here to Skip to main content
15,921,694 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am trying to clear the treeview but it is throwing an exception "ObjectReference not set" when i checked the Treeview.node.count it is 1

so iam thinking how come this error occur.


C#
private void LoadLayout(Layout result) {
   TreeView view = dtLayout;
      view.SuspendLayout();
     view.Nodes.Clear();---------------------------->here the exception occur
    TreeNode root = new TreeNode("root",result.Label,result.ImageKey) { Tag = result };
     view.Nodes.Add(root);
     AddTreeNodes(result.Children,root);
     root.Expand();
     view.SelectedNode = SelectNode(view.Nodes[0]);
     view.ResumeLayout();
   }
Posted
Comments
Richard MacCutchan 16-May-12 8:12am    
Either view or view.Nodes is null. Use your debugger to find out which.

1 solution

This error happens when you try to use a property or call a method of an object that is null. More details: here[^]

A simple use of Visual studio DEBUGGER can tell you the object because of which it is happening. Just look at the stack trace and put a debugger on that line. Check the objects of that line and see if any one is null and you are trying to use that objects property. Handle the same.



Check if view or view.Nodes is null. Handle it before using.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900