0

I'm having trouble constructing a huffmanTree without using frequencies, the user will enter a character and a binary number directly to the huffmanTree. Based on the user input, if the first input of the binary is 0, it should go left, if else it's 1, it should go to the right. When inserting the second binary code the program should check if that place is a leaf or not. If it's a leaf, insert. If not return a message as that place is taken.

I have came up with an idea that is returning a error..

Exception in thread "main" java.lang.NullPointerException at ideas.ideas.insert(ideas.java:70)

import java.util.LinkedList;
import java.util.Scanner;


  class Node {

    char data;
    int idata;
    boolean isCreated = false;
    Node leftChild, rightChild;

    public Node(char data) {
        this.data = data;
        leftChild = new Node();
        rightChild = new Node();
    }

    public Node() {
    }

}


public class ideas{

    Node root = new Node();

    public ideas(){

        root.isCreated = true;

    }

    void insert(char data, String bin) {
        Node newNode = new Node();
        newNode.isCreated=true;

        if (!root.isCreated) {
            System.out.println("It never enters here, since the root is always empty and is created with the constructor ");
            root = newNode;
            root.isCreated = true;
        } else {
            System.out.println(""); 
            Node current = root;
            Node parent = current;
            int i = 0;

            while (i < bin.length()) {

                parent = current;
                if (bin.charAt(i) == '0') {
                    System.out.println("Parent leftChild is : "+newNode.isCreated);
                    current = current.leftChild;
                   // System.out.println("Current left child is : "+current.isCreated);
                           if (current.isCreated == false) {

                        parent.leftChild = newNode;
                        current.isCreated = true;
                        // current.idata = 0;
                        //return;
                        i++;
                    }
                } else {
                    current = current.rightChild;


                    if (current.isCreated == false) {
                        parent.rightChild = newNode;
                        current.isCreated = true;
                        //current.idata = 1;
                        //return;
                        i++;
                    }
                }
            }

            current.data = data;
             System.out.println("the data is "+current.data);
        }
    }

    void preOrder(Node current) {
        if (current.isCreated == false) {


                System.out.println(current.data);

            preOrder(current.leftChild);
            preOrder(current.rightChild);

        }

    }

    public static void main(String[] args) {
        System.out.println("Enter a character");
        System.out.println("Enter its binary code");
        ideas t = new ideas();
        t.insert('e', "10101");
    //    t.insert('a', "00101");
        t.preOrder(t.root);

    }

}
LoD
  • 53
  • 8
  • _"I have came up with an idea that is returning a error"_ And the error is...? – Michael Mar 15 '16 at 09:10
  • @Michael when running the program it gives a null pointer exception – LoD Mar 15 '16 at 09:12
  • 2
    Then include the full stack trace in your question. – Michael Mar 15 '16 at 09:13
  • Go to line 70 in ideas.java and fix whatever you're doing that causes you to dereference a null reference. – Michael Mar 15 '16 at 09:32
  • I suspect it is because you are calling `current = current.rightChild`, when `current` is a node created using the default constructor, which does not initialize the `left`/`rightChild` fields, so the `current.isCreated` line yields a NPE. You should initialize the `left`/`rightChild` fields before that line. – Andy Turner Mar 15 '16 at 09:57
  • Please indicate line 70 in the code. – Andy Turner Mar 15 '16 at 09:59
  • @AndyTurner the problem is if (current.isCreated == false) { – LoD Mar 15 '16 at 10:09
  • @LoD in that case it is exactly the problem I have described: "so the `current.isCreated` line yields a NPE". – Andy Turner Mar 15 '16 at 10:12
  • @AndyTurner would you recommend to initialize them inside the node class or the idea class ? ..thanks in advance – LoD Mar 15 '16 at 11:04
  • Just think about what might happen if you create a `Node` in the constructor of a `Node`... – Andy Turner Mar 15 '16 at 11:05

0 Answers0