0

I have a quadtree which has a remove method, and every time I remove an item from the game I call this method. The problem is that in the foreach loop, the child variable is null. How can I bypass this method so if the variable is null it won't give me an error?

private void CollapseChildren(RectangleTreeNode node)
    {
        foreach (RectangleTreeNode child in node.Children)
        {
            if (child == null)
            {
                return;
            }

            while (child.Items.Count > 0)
            {
                MoveUp(child.Items[0]);
            }
        }

        node.Children = null;
    }

Here's the remove method.

private void Remove(RectangleTreeNode node, RectangleTreeItem item)
    {
        node.Items.Remove(item);
        item.Parent = null;

        while (node != null)
        {
            node.ItemCount--;
            if (node.ItemCount < 6)
            {
                CollapseChildren(node);
            }
            node = node.Parent;
        }
    }

If the value is null then I shouldn't have to loop through the children in nodes.Children because if I do loop through then I get the error at the child variable. Meaning the node doesn't have a child.

3
  • If the value is null then I shouldn't have to loop through the children in nodes.Children because if I do loop through then I get the error at the child variable. Meaning the node doesn't have a child Commented Oct 11, 2014 at 2:37
  • 1
    Our company policy is never to set collections null like you do with node.Children = null, always create an empty collection like node.Children = new RectangleTreeNode[0]. This avoids errors and simplifies logic considerably Commented Oct 11, 2014 at 3:14
  • @DourHighArch: That is a good policy. Also, you can use the same empty array for every empty collection of that type, and avoid the memory allocation. It's an empty array, it's not going to change and one is as good as another. Commented Oct 11, 2014 at 3:27

2 Answers 2

1

You're already checking whether or not each child is null.

If there's a risk of node.Children being null, then check for that too, before accessing it:

private void CollapseChildren(RectangleTreeNode node)
{
    if (node.Children == null)
        return;

    foreach (RectangleTreeNode child in node.Children)
    {
       ...
       ...
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Still get the error. So it must not be in the method.
I tried the answer below and it worked! I just had them in the wrong order
I missed the if(child == null) inside the foreach loop. I put it outside for some reason
1

You'll have to check if node.Children is null prior to invoking the foreach loop, so your method becomes:

private void CollapseChildren(RectangleTreeNode node)
{
    if (node.Children == null)
    {
        // exit the method, or do whatever is appropriate when node.Children is null
        return;
    }

    foreach (RectangleTreeNode child in node.Children)
    {
        if (child == null)
        {
            return;
        }

        while (child.Items.Count > 0)
        {
            MoveUp(child.Items[0]);
        }
    }

    node.Children = null;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.