0

This is my code for a binary tree program I am doing for an algorithms class. I keep getting a null exception error thrown, but I have no clue what's causing it.

namespace Tree
{
    class CallTree
    {
        static void Main(string[] args)
        {

            BNode HE = new BNode(0, "House Entrance");

            BNode LowerHallway = new BNode(0, "Lower Hallway");
            BNode UpperHallway = new BNode(0, "Upper Hallway");
            HE.setLeft(LowerHallway);
            HE.setRight(UpperHallway);

            BNode Lounge = new BNode(0, "Lounge");
            BNode Kitchen = new BNode(0, "Kitchen");
            LowerHallway.setLeft(Lounge);
            LowerHallway.setRight(Kitchen);

            BNode Balcony = new BNode(0, "Balcony");
            Kitchen.setRight(Balcony);

            BNode Study = new BNode(0, "Study");
            BNode MasterBedroom = new BNode(0, "Master Bedroom");
            UpperHallway.setLeft(Study);
            UpperHallway.setRight(MasterBedroom);

            BNode GuestBath = new BNode(0, "Guest Bath");
            BNode GuestBedroom = new BNode(0, "Guest Bedroom");
            Study.setLeft(GuestBath);
            Study.setRight(GuestBedroom);

            BNode PrivateBath = new BNode(0, "Private Bath");
            BNode Closet = new BNode(0, "Closet");
            MasterBedroom.setLeft(PrivateBath);
            MasterBedroom.setRight(Closet);

            HBinaryTree HBinaryTree = new HBinaryTree(HE);
            BNode rootNode = HBinaryTree.GetRoot();

I get an exception for 'rootNode' here V

            HBinaryTree.preOrder(rootNode);
            //HBinaryTree.inOrder(rootNode);
            //HBinaryTree.postOrder(rootNode);

            Console.ReadKey();
        }
    }
    //definition of node in a binary tree
    public class BNode
    {
        public string room;
        public int treasure;

        public BNode left, right;//left child and right child

        public BNode(int item, string room)
        {
            treasure = item;
            left = null;
            right = null;
        }

        public BNode(int item, string room, BNode leftNode, BNode rightNode)
        {
            treasure = item;
            left = leftNode;
            right = rightNode;
        }

        public void show()
        {
            Console.Write(treasure);
        }
        //Is it interial node?
        public bool isInner()
        {
            return left != null || right != null;
        }
        //Is it a leaf node?

        public bool isLeaf()
        {

            return left == null && right == null;
        }

        //Does it have a left child?
        public bool hasLeft()
        {
            return left != null;
        }

        //Does it have a right child?
        public bool hasRight()
        {
            return right != null;
        }
        //Set its left child to be newLeft
        public void setLeft(BNode newLeft)
        {
            left = newLeft;
        }

        //Set its right child to be newRight
        public void setRight(BNode newRight)
        {
            right = newRight;
        }

        //return data value
        public int getValue()
        {
            return treasure;
        }

        //set data value
        public void setValue(int newValue)
        {
        treasure = newValue;
        }

    }

    //definition of a proper binary tree
    class HBinaryTree
    {

        public BNode root;  //root of the tree

        public HBinaryTree()
        {
            root = null;
        }

        public BNode GetRoot()
        {
            return root;
        }

        public HBinaryTree(BNode rootNode) // constructor
        {
            root = rootNode;
        }
        // PreOrder traversal
        public void preOrder(BNode root)
        {

And more exception errors for 'root' in these 4 lines V

            root.show();
            if (root.isInner())
            {
                preOrder(root.left);
                preOrder(root.right);
            }

        }

        //// InOrder traversal
        //public void inOrder(BNode root)
        //{

        //    if (root.isInner())
        //    {
        //        inOrder(root.left);
        //    }
        //    root.show();
        //    if (root.isInner())
        //    {
        //        inOrder(root.right);
        //    }
        //}

        //PostOrder traversal
        //public void postOrder(BNode root)
        //{

        //    if (root.isInner())
        //    {
        //        postOrder(root.left);
        //        postOrder(root.right);
        //    }
        //    root.show();
        //}
   }
}

I would like it if someone could help me deduce what is causing the exceptions for 'root' and 'rootNode' to be thrown. It states they are null but I can't understand why it states that.

5
  • A complete example that illustrates the problem might help. In these out-of-context examples, I do not see anything wrong. Failing that, you might try debugging your code. Find the point at which you think the null variable should be assigned, and figure out whether it is assigned and whether it really is the variable you're dereferencing. I guess it isn't. Commented Mar 4, 2015 at 0:25
  • @phoog The code posted does demonstrate the problem. Commented Mar 4, 2015 at 0:25
  • @LarsTech I guess I didn't look at it closely enough. That's the problem with including large code examples in SO questions. Commented Mar 4, 2015 at 0:26
  • @phoog You appear to be the only one that had a problem reading it. Commented Mar 4, 2015 at 10:29
  • @Remnant_Echo but, I wonder, how many people didn't even bother to try? Commented Mar 4, 2015 at 18:25

1 Answer 1

3

Your preOrder function recursively calls itself for the left and right nodes off every node in your tree if isInner returns true. isInner returns true if either of its nodes are non-null, but your kitchen has only a right branch, and its left is null. So when that function reaches the kitchen, it sees isInner is true and it calls itself with the left branch off kitchen which is null. You need to check for null for left and right individually.

Sign up to request clarification or add additional context in comments.

4 Comments

Yep, this example fails on the fifth recursive call. This can be seen when debugging.
I can't see anything when I debug, only errors, it doesn't give me the rest of the tree. But thank you, I will try that out.
Ok I managed to get it to stop tripping the exception, but all it displays is 000, nothing else.
Then it sounds like you have a different question, since the null exception issue has been resolved.

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.