0

I'm trying to sort a BST alphabetically and I wrote an insert method if compareTo==-1 it goes to root.setLeft() if compareTo==1 goes to root.setRigth() but obviously when I run the program it doesn't start with any value so it's null and I'm getting NullPointerException, I know I have to start the comparison after I values but I can't figure out how to do that, also if I put a valid value in the constructor it doesn't insert values after that.

class BST{

    Node insertNames(Node root, int data, String name, double salary){
                if(root==null){
                    root=new Node();
                    root.setData(data);
                    root.setName(name);
                    root.setLeft(null); root.setRight(null);    
                    }else{
                        if(name.compareTo(root.getName())==-1) root.setLeft(insertNames(root.getLeft(),data, name, salary));
                        else if(name.compareTo(root.getName())==1) root.setRight(insertNames(root.getRight(),data, name, salary));
                }
                return root;
            }    

 void printInorder(Node node) 
        {
            if (node == null)
                return;

            /* first recur on left child */
            printInorder(node.left);

            /* then print the data of node */
            System.out.print(node.getName() + " ");

            /* now recur on right child */
            printInorder(node.right);


        }

}

class Node{

int data;
    Node left;
    Node right;
    String name;

    public void setName(String name){
        this.name=name;
    }

    public String getName(){
        return name;
    }

    public Node(){

    }
    public int getData(){
        return data;
    }

    public void setData(int data){
        this.data=data;
    }

    public Node getLeft(){
        return left;
    }

    public void setLeft(Node left){
        this.left=left;
    }

    public Node getRight(){
        return der;
    }

    public void setRight(Node Right){
        this.right=right;
    }

class main{
    public static void main(String[] args){
        BST b1=new BST();
        Node root=new Node(); 
/*Here the root is null, I think this causing the NullPointerException, but if a have a constructor with parameters it doesn't insert any name instead the one that I put in the parameter*/
        b1.insertNames(root, 0, "Roy", 0);
        b1.insertNames(root, 0, "Joseph", 0);
        b1.printInorder(root);
    }

}
  • 1
    Please provide a real [mcve]. – GhostCat May 12 '17 at 03:51
  • It's done, I added 2 more classes. – Valenzuela May 12 '17 at 05:00
  • It's impossible for `root` to be null at the location you've pointed to. – shmosel May 12 '17 at 05:02
  • Then why if I run it I get null instead of the names? – Valenzuela May 12 '17 at 05:03
  • What does that mean? Where are you "getting null"? – shmosel May 12 '17 at 05:08
  • When I print using the `printInOrder` method it only prints `null` that's why I assume that the value has to be null. – Valenzuela May 12 '17 at 05:19
  • Sorry, but this is not programming school were we hold your hand with those super basic stuff. Please read the DUP question and the answers. If you dont understand that, read it again. Beyond that, your code is simply **over complicating things**. You do not *insert* **nodes** into your tree, you insert **data**. The **tree** class then creates the required new nodes. Thus: start by clarifying the signatures of your methods. – GhostCat May 12 '17 at 06:25
  • This is not a duplicate of NullReferenceException, it is a duplicate of one about learning binary trees, sorry no link. Nonetheless, I'm voting to leave closed, just hoping someone can add a more relevant link. – Kelly S. French May 12 '17 at 15:28

0 Answers0