- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我对 TreeView 节点有疑问。当我点击某些节点时,出现未处理的异常并显示“对象引用未设置到对象的实例”。
我认为发生此异常是因为我在 mouseclick 事件中使用了 treeview.node.parent 和 treeview.node.firstnode 方法。
你能帮我解释一下为什么会出现这个异常吗?
我认为错误在这个片段中:
private void treeNode_AfterSelected(object o, TreeNodeMouseClickEventArgs e )
{
//
if (e.Node.FirstNode != null && e.Node.Parent!=null && e.Node.Parent.Text == "Tables")
{
this.Controls.Remove(dg);
this.dg= dal.showTable(e.Node.Text,e.Node.Parent.Parent.Text);
this.dg.Location = new System.Drawing.Point(this.tr.Width + 1, this.menuStrip1.Height + 2);
this.dg.Size = new System.Drawing.Size(n - dg.Location.X, 300);
this.dg.BackgroundColor = System.Drawing.Color.White;
this.tableName = e.Node.Text;
this.Controls.Add(dg);
}
else if (e.Node.FirstNode == null && e.Node.FirstNode.Text == "Tables")
{
dal.changeDatabase(e.Node.Text);
}
}
p.s 对不起英语不好
最佳答案
如果您将点击父节点(第一级)然后调用
node.Parent.SomeMethod
你会得到 NullReference 异常,因为它的 Parent 是 null
进行一些验证以检查 Parent 是否不为 null
if(node.Parent != null)
{
// do stuff
}
node.FirstNode 的情况相同 - 如果此节点没有子节点,它将返回 null
,因此也对此进行验证
if(node.FirstNode != null)
{
// do stuff
}
编辑:在您的代码段 e.Node.Parent.Parent
中,一些父级可以为 null,而 e.Node.FirstNode
可以为 null,因此您以异常结束
if (e.Node.Parent != null && e.Node.Parent.Text == "Tables")
{
this.Controls.Remove(dg);
if(e.Node.Parent.Parent != null)
{
this.dg= dal.showTable(e.Node.Text,e.Node.Parent.Parent.Text);
this.dg.Location = new System.Drawing.Point(this.tr.Width + 1, this.menuStrip1.Height + 2);
this.dg.Size = new System.Drawing.Size(n - dg.Location.X, 300);
this.dg.BackgroundColor = System.Drawing.Color.White;
this.tableName = e.Node.Text;
this.Controls.Add(dg);
}
}
else if (e.Node.FirstNode != null && e.Node.FirstNode.Text == "Tables")
{
dal.changeDatabase(e.Node.Text);
}
关于c# - 为什么在winforms应用中点击treeview节点会出现异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6169395/
我需要你的帮助!我在它们之间放置了随机数量的 div。 Item description Item description Item description Item
我有两个 NSDates,时间格式为“h:mm a”(即 6:00 AM 和 8:00 PM)。 我试图找出这两个时间之间的中点是什么时间。 对于上面的示例,早上 6:00 和晚上 8:00 之间的中
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我正在寻找一种有效的算法来检查一个点是否在 3D 中的另一个点附近。 sqrt((x2-x1)^2 + (y2-y1)^2 + (z2-z1)^2) < radius 这似乎并不太快,实际上我不需要这
我可以让 pandas cut/qcut 函数返回 bin 端点或 bin 中点而不是一串 bin 标签吗? 目前 pd.cut(pd.Series(np.arange(11)), bins = 5)
我是一名优秀的程序员,十分优秀!