Treeview.nodes.clear引发异常 [英] Treeview.nodes.clear is throwing exception

查看:70
本文介绍了Treeview.nodes.clear引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试清除树视图,但是当我检查Treeview.node.count为1
时,它抛出异常"ObjectReference not set"
所以我想着怎么会发生此错误.


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.


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();
   }

推荐答案

当您尝试使用属性或调用空对象的方法时,会发生此错误.更多详细信息:此处 [
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.


这篇关于Treeview.nodes.clear引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆