[英]Why is this object null after being assigned?
完整课程在这里: https : //gist.github.com/archer884/453c9937afa2dcb74f24
在我的班级(某种树)中,我有一个Node<T> _root;
在Add方法中,此变量被分配给一个名为简单node
的局部变量,然后我们在node
上做了一些工作,如下所示。
while (node != null)
{
nodeDimensions = Dimensions(node.Item);
if (dimensions.SequenceEqual(nodeDimensions))
throw new ArgumentException("An element with the same key already exists in the tree.");
dimension = depth % dimensions.Count;
comparison = dimensions[dimension].CompareTo(nodeDimensions[dimension]);
if (comparison <= 0)
{
node = node.LeftChild;
continue;
}
else
{
node = node.RightChild;
continue;
}
}
node = new KdTreeNode<TItem>(item);
因此,对我来说,这似乎是我只是在从该变量中分配复活节彩蛋,但我的测试用例却不同意: Assert.NotNull(tree.Root);
总是失败。
编辑:对于将来的读者,我将注意以下内容。
if (_root == null)
{
_root = new KdTreeNode<TItem>(item);
return;
}
这样,当我们开始分配内容时,_root不会引用null。 我认为。 也许其他家伙之一会澄清。
在代码中的任何地方都没有实际分配给_root
字段,因此tree.Root
始终为null
。 您需要将_root
分配给Add
计算的值
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.