Treeview ChildNode problem

John Noble 1 Reputation point

Hi folks,

I am having difficulty trying to access child nodes when my form loads.

My treeview I have 17 root nodes. Each has root node has several children. Each node has a unique name which is set as design time. o1, o2, o3 ..... o250.

In my form_Load event I am calling a routine that specifies what text to give to each node (root and children).

private void Main_Load(object sender, EventArgs e)

public void SetUpText()
            // Root Nodes
            treeView1.Nodes["o1"].Text = Utilities.Strings.DataMaintenance;
            treeView1.Nodes["o32"].Text = Utilities.Strings.Invoicing;
            treeView1.Nodes["o237"].Text = Utilities.Strings.PurchaseLedger;
            treeView1.Nodes["o4"].Text = Utilities.Strings.ProductCostings;
            treeView1.Nodes["o210"].Text = Utilities.Strings.Boats;
            treeView1.Nodes["o42"].Text = Utilities.Strings.Finance;
            treeView1.Nodes["o7"].Text = Utilities.Strings.SalesAnalysis;
            treeView1.Nodes["o2"].Text = Utilities.Strings.Quotations;
            treeView1.Nodes["o5"].Text = Utilities.Strings.SalesLedger;
            treeView1.Nodes["o3"].Text = Utilities.Strings.Orders;
            treeView1.Nodes["o157"].Text = Utilities.Strings.FishBook;
            treeView1.Nodes["o10"].Text = Utilities.Strings.Utilities;
            treeView1.Nodes["o150"].Text = Utilities.Strings.NominalLedger;
            treeView1.Nodes["o8"].Text = Utilities.Strings.Intrastats;
            treeView1.Nodes["o9"].Text = Utilities.Strings.CreditControl;
            treeView1.Nodes["o220"].Text = Utilities.Strings.CustomerBoard;
            treeView1.Nodes["o42"].Text = Utilities.Strings.Finance;

            // Children of 1st Node
            treeView1.Nodes["o152"].Text = Utilities.Strings.General; // Error here
            treeView1.Nodes["o153"].Text = Utilities.Strings.NominalCodes;
            treeView1.Nodes["o154"].Text = Utilities.Strings.Warehouses;
            treeView1.Nodes["o155"].Text = Utilities.Strings.Banks;

The root nodes all update with the correct text. My problem is that I cant get access to the child nodes. I get error message "Object reference not set to an instance of an object".

Any ideas what might be causing this ?


Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,854 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Viorel 112.9K Reputation points

    Try two methods:

    treeView1.Nodes["o1"].Nodes["o152"].Text = Utilities.Strings.General;


    treeView1.Nodes.Find("o152", true)[0].Text = Utilities.Strings.General;

    1 person found this answer helpful.
    0 comments No comments

  2. Adnan Dedic 406 Reputation points

    The problem is that you are trying to directly access nodes from the wrong collections.
    For example "o152" is not in the treeView1.Nodes, but in the o1.Nodes collection.

    Anyway, simpler, more flexible and safer solution can be created with a simple recursive method and the dictionary.

    public Form1() {  
    private void Form1_Load(object sender, EventArgs e) {  
    private void SetUpText() {  
        var nodeNameDictionary = BuildNameDictionary();  
        SetUpNodesText(treeView1.Nodes, nodeNameDictionary);  
    // You can build dictionary here or in the utilities.  
    private Dictionary<string, string> BuildNameDictionary() {  
        return new Dictionary<string, string> {  
            // { "o1", Utilities.Strings.DataMaintenance },  
            // { "o32", Utilities.Strings.Invoicing; },  
            // Test values:  
            { "o1", "Data Maintenance" },  
            { "o152", "General" },  
            { "o153", "Nominal Codes" }  
    private void SetUpNodesText(TreeNodeCollection nodes, Dictionary<string, string> nodeNameDictionary) {  
        foreach (TreeNode node in nodes) {  
            var nodeName = node.Name;  
            if (nodeNameDictionary.ContainsKey(nodeName)) {  
                node.Text = nodeNameDictionary[nodeName];  
            SetUpNodesText(node.Nodes, nodeNameDictionary);  


    0 comments No comments