Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Alright, I'm having hard time figuring our how I can assign default values to the elements of the list (Trees and nodes) so I don't get NullReferenceException.

The basic idea of the code is:
List contains from Trees, each tree contains 2 nodes, each node contains 2 sub-nodes.
______________________________
|...L.N...................R.N..........|
|.../..\...................../ \..........| <---Each List[i] contains the following.
|L.N...R.N........L.N...R.N......|
------------------------------


Each node contains Name(string) and Size(Int) Data.
code:
C#
class Program
{
    static void Main(string[] args)
    {
        List<Tree> TreeList = new List<Tree>();

        Tree newtree = new Tree();
        Branch newbrach = new Branch();

        // user can add any number of new trees to the list
        // when tree is added, user can edit the default values of the tree

        foreach (Tree item in TreeList)
        {
            //loop to print out all values
        }

    }

 }


C#
class Tree
    {
        public Branch Left = new Branch();
        public Branch Right = new Branch();
    }


C#
class Area
{
    public string Name;
    public int Size;
}


C#
class Branch
{
    public Area Area = new Area();
    public Branch Right;
    public Branch Left;
}


I must be missing a constructor of some sort, because if I'm adding new tree the the list and I'm trying to directly edit it via List[i].Left.Area.Name = ... I get NullReferenceExeption.

Basically, what I want is - whenever I add a new Tree to the list, each node should have it's default values set to 0 or something that can be manually changed later.
Posted

I'd suggest to read this: Binary Trees and BSTs[^] and this: Implementing a Binary Tree[^]. The second article is about VB, but it might help you to understand how to achieve that.
 
Share this answer
 
No, you don't. The problem is that you aren't checking that each Branch actually exists: so when you get this far:
C#
List[i].Left.
the Left Branch is null - so when you try to use it you get an exception. This isn't a fault: a null would be normal to indicate "end of tree".
What you need to look at doing is something like:
C#
Branch left = List[i].Left;
Branch right = List[i].Right;
if (left != null)
   {
   ...
   }
if (right != null)
   {
   ...
   }
I'd also suggest that you create a "PrintNode" method and use recursion to follow the Left and then Right branches until you get to the null value indicating "no more nodes"
 
Share this answer
 
Comments
[no name] 9-May-15 5:21am    
I see, thank you! That that being said, it there any way for me to make so all branches are not null but have default values set to, for example "empty"?
Say I create a new tree - all nodes and sub nodes have preset values of "empty".
OriginalGriff 9-May-15 5:39am    
Yes, you could - but you'd need to add a bool flag to the Branch class and then you'd need to check that! And that causes problems, because the "Empty" tagged branches would still have a Left and Right branch reference, which would also need to be filled with "Empty" tagged Branches, which... you get the idea.
It's a much, much better idea to check the nulls - it is something you should be doing by default anyway! :laugh:

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