Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am dynamically populating a 3 level tree view in VB. I can add a new first level node and set a object variable that allows me to reference it to change the value and other properties, but I can't seem to do the same with the child node. This works for the first level:
tvLevels.Nodes.Add(New TreeNode(strNodeName))
nodeLevel1 = tvLevels.FindNode(strNodeName)
nodeLevel1.Value = datareader("AALevelID")


but this doesn't work for child nodes:
nodeLevel1.ChildNodes.Add(New TreeNode(strNodeName))
nodeLevel2 = tvLevels.FindNode(strNodeName)
nodeLevel2.Value = datareader("AALevelID")
I get the error
"Object reference not set to an instance of the object"

The objects are declared as:
Dim nodeLevel1 As New TreeNode
Dim nodeLevel2 As New TreeNode

Any help would be appreciated.
Posted
Comments
Sergey Alexandrovich Kryukov 2-Mar-12 9:53am    
Why FindNode, in principle? You are populating, not searching...
--SA

All wrong. When you write New TreeNode(strNodeName), it already returns you the node. You are populating. All object are yours, how could you come to the idea of using FindNode? Why searching for anything at all?

Your FindNode returns you null, and you are trying to de-reference the null in next line via nodeLevel2.Value which throws the exception.

—SA
 
Share this answer
 
Comments
cahigg 2-Mar-12 10:09am    
I just want to be able to set an object variable equal to the child node I just created so that I can set the value to something other than the displayed text and so that I can easily add a child node to that child node. I need to know how to find a child node based on the displayed text or assigned value or even the index of the node. If you can tell me how to do this I would appreciate it. The object variables are defined in the beginning of the subroutine and the nodes are populated from looping through the results of a query.
Sergey Alexandrovich Kryukov 2-Mar-12 14:17pm    
Look, population is very simple. You create a node, insert it. If you need to insert something in the that new node, don't forget its reference. When you create a node, fist save the result in a temporary variable and use that variable in next step. So easy.

What problem you still may have?
--SA
Not pretty but it works:



nodeLevel1.ChildNodes.Add(New TreeNode(strNodeName))

For Each nodeLevel1 In tvLevels.Nodes
For Each nodeLevel2 In nodeLevel1.ChildNodes
If nodeLevel2.Text = strNodeName Then
nodeLevel2.Value = datareader("AALevelID")
End If
Next
Next
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900