【发布时间】:2018-11-09 23:16:46
【问题描述】:
遍历一些树列表复选框节点,并尝试将一些子节点设置为 true。当我尝试设置一个时,无论是真还是假,都会导致异常。
'childNode.Checked' threw an exception of type 'System.NullReferenceException'
我错过了什么明显的东西吗?
谢谢
private void FluidFilterTree_AfterCheck(object sender, TreeViewEventArgs e)
{
TreeNode ActiveNode = e.Node;
TreeNode childNode = ActiveNode.FirstNode;
if (ActiveNode.Parent == null) // if null it is the parent, check all children
{
for (int x = 0; x < ActiveNode.GetNodeCount(false); x++) // loop through children nodes
{
childNode.Checked = false; //?
childNode = childNode.NextNode;
}
}
MessageBox.Show(e.Node.Text);
}
对象 childNode 已设置并且正在工作,我在该行上有一个断点,它会一直工作,直到它尝试设置 .checked=true。所以我不确定这是标准“这个异常是什么意思”的重复我理解这个异常但不确定为什么它在这种情况下是相关的,因为我知道 childNode 和 true 都存在
【问题讨论】:
-
@KenWhite,对象
childNode已设置并且正在工作,我在该行上有一个断点,它可以工作,直到它尝试设置.checked=true。所以我不确定这是标准“这个异常是什么意思”的副本 -
显然它不是 set and working 在引发异常的迭代中,否则您将不会得到异常。您阅读了链接副本标题中句子的前半部分(What is),但没有阅读后半部分(how do)。
-
` if (childNode == null) { MessageBox.Show("node is null"); } childNode.Checked = true;` 仍然抛出问题
-
所以你认为这个例外是虚构的?它显然是在另一个循环中。