0

looping through some treelist checkbox nodes, and trying to set some child nodes to true. when I try set one, either true or false it causes an exception.

'childNode.Checked' threw an exception of type 'System.NullReferenceException'

Am i missing something obvious?

thanks

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

The object childNode is set and is working, I have a breakpoint on the line and it works until it tries to set .checked=true. So I'm not sure this is a duplicate of the standard "what does this exception mean" I understand the exception but not sure why it is relevant in this case as I know both childNode and true exist

Breakpoint before changing checked Breakpoint before changing <code>checked</code>

James Lingham
  • 419
  • 1
  • 3
  • 17
  • Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Ken White May 31 '18 at 02:43
  • @KenWhite, The object `childNode` is set and is working, I have a breakpoint on the line and it works until it tries to set `.checked=true`. So I'm not sure this is a duplicate of the standard "what does this exception mean" I understand the exception but not sure why it is relevant in this case as I know both `childNode` and `true` exist – James Lingham May 31 '18 at 02:46
  • Clearly it's not *set and working* in the iteration that is raising the exception, or you wouldn't be getting the exception. You read the first half of the sentence in the title of the linked duplicate (*What is*) but failed to read the second half (*how do*). – Ken White May 31 '18 at 02:48
  • ` if (childNode == null) { MessageBox.Show("node is null"); } childNode.Checked = true;` still throws up the problem – James Lingham May 31 '18 at 02:54
  • So you think the exception is imaginary? It's apparently in another pass through the loop. – Ken White May 31 '18 at 02:55
  • I do not think it is imaginary, I'm unsure how the object can be valid and turn to null while i try to set a bool value. I can read the value of checked, but when i set it its becomes null. I do not understand why which is why I have asked. – James Lingham May 31 '18 at 02:58
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/172129/discussion-between-james-lingham-and-ken-white). – James Lingham May 31 '18 at 03:10
  • My bad, I misread your post :/. – ChiefTwoPencils May 31 '18 at 03:47
  • I appreciate your effort - I must go to sleep now (5AM) doesn't look like this problem is going anywhere soon. Thanks for the help. – James Lingham May 31 '18 at 03:49
  • Can you provide more detail and stacktrace of that exception? I assume your ActiveNode is in a thread-safe case (or not?) thus the NullreferenceException is only possible at `childnode.Text="help"`. – joe May 31 '18 at 05:13
  • @joe I just posted the solution, but essentially as the childNode was being updated, it was causing the event `afterchecked` to be ran again, this time using the childNode as the activeNode (as this was the node just updated) which seems to have been causing the issue – James Lingham May 31 '18 at 10:07

3 Answers3

1

You're assuming that ActiveNode has children.

This isn't necessarily true. If there are no children, ActiveNode.FirstNode is null and you can't reference the Checked property.

Terry Carmen
  • 3,720
  • 1
  • 16
  • 32
  • Thanks. ActiveNode will always have children in this example. – James Lingham May 31 '18 at 02:50
  • Regardless of how sure you are that it *should* exist, you can't ever be sure that any object *actually* exists without checking. Making assumptions about the existence of nodes at any point in time is just leaving traps for future users and programmers. Before working on an object, you need to make sure it's actually there. – Terry Carmen May 31 '18 at 14:00
1

Agree with Terry Carmen, you assuming that childNode will never be null. But, as you can see that NullReferenceException is thrown, it is not true. How about this, more safe example:

private void FluidFilterTree_AfterCheck(object sender, TreeViewEventArgs e)
{
    var activeNode = e.Node;
    if (activeNode.Parent == null)
        foreach (TreeNode child in activeNode.Nodes)
            child.Checked = false;

    MessageBox.Show(e.Node.Text);
}
vasily.sib
  • 3,871
  • 2
  • 23
  • 26
  • thanks for the reply, I just tried this and it is still throwing an exception. I know in the scenario I am looking for that there will always be a childnod. – James Lingham May 31 '18 at 03:03
  • 1
    It could be me, but I fail to see how this can throw an exception. Is there maybe any other code the fires when the checked property changes or something ? – GuidoG May 31 '18 at 08:34
  • I just posted the solution, but essentially as the childNode was being updated, it was causing the event `afterchecked` to be ran again, this time using the childNode as the activeNode (as this was the node just updated) which seems to have been causing the issue – James Lingham May 31 '18 at 10:07
1

The answer seems really obvious now. As the code was executing, and childNode.checked was being modified, it was then calling the function to be ran again, this time on the updated node, as this being updated would called the afterchecked event.

Wrapping it in

if (e.Action.ToString() == "ByMouse")
{
    // code here
}

seems to be working and prevents it being called multiple times.

James Lingham
  • 419
  • 1
  • 3
  • 17
  • How does it pass `ActiveNode.Parent == null` then if it is a childnode passed in? – joe Jun 01 '18 at 05:56