1

Can someone help me find the issue? I am trying to assemble a binary search tree from a list I am new to all of this sorry It trys to assemble A node list then makes a queue from the output and uses the queue to assemble the tree

Here is the code

Main.java

    static List<Node> assembleNodeList(List<Double> items) {
        ArrayList<Node> nodelist = new ArrayList<Node>();
        for (int i = 0; i < items.size(); i++) {
            nodelist.add(new Node(items.get(i)));
        }
        return nodelist;
    }
    static Queue assembleQueue(List<Node> nodelist) {
        Queue<Node> queue = new LinkedList();
        for (int i = 0;i<nodelist.size();i++) {
            queue.add(nodelist.get(i));
        }
        return queue;
    }
    static BinaryTree assembleTree(Queue<Node> queue,BinaryTree tree,Node current) {
        if (queue.isEmpty()) {
            return tree;
        }
        if (tree.root == null) {
            tree.root = queue.peek();
            queue.remove();
        }
        Node item = queue.peek();
        if (item.value < current.value) {
            current.left = item;
            queue.remove();
        } else if (item.value >= current.value) {
            current.right = item;
            queue.remove();
        }
        return assembleTree(queue,tree,current);
    }
    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree();
        double targetVal = 0.5;
        tree.root = new Node(5);
        tree.root.left = new Node(3);
        tree.root.right = new Node(7);
        tree.root.left.left = new Node(1);
        tree.root.left.left.left = new Node(0.5);
        tree.printInorder(tree.root);
        try {
            System.out.println(treeTraverse(targetVal,tree,tree.root));
        } catch (NullPointerException e) {
            System.out.println(false);
        }
    }
}
  • 2
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Jannik Oct 06 '19 at 20:29
  • I'm pretty sure that it has a line where the error occurs. You can start investigating from there. – Francis G Oct 07 '19 at 01:25
  • Hi Sean! Welcome to StackOverflow . Try to create a [minimal reproducable](https://stackoverflow.com/help/minimal-reproducible-example) example. I find, in doing so, I often find the source of my issues and it's faster to debug. If you don't, it helps others help you. – Ben Aubin Oct 07 '19 at 01:32

0 Answers0