Can someone please tell me why I keep getting a stack overflow when I try to create nodes in a priority queue?

class Node
   {
   public int frequency;              // data item (key)
   public char character;           // data item
   public Node leftChild;         // this node's left child
   public Node rightChild;        // this node's right child
   public HuffmanTree leaf;
   public HuffmanLeaf leaf1; 
   //LinkedList<Integer> bob;
   //bob = new LinkedList<Integer>();
   PriorityQueue<Node> trees;
   public Node()                  //Constructor
   {
   }
   public Node(int passed_frequency, char char_character)                  //Constructor
   {
       //super(frequency);
       System.out.println("Start of Node(int passed_frequency");
       /*for (int i = 0; i < 1; i++)
       {
           //System.out.println("passed frequency " + passed_frequency +
                   //" char_character " + char_character);
       }*/
       int work = passed_frequency;
       char grr = char_character;
       trees.offer(new Node(work, grr));
       System.out.println("--------------------------------------------");
       //break;
   }
}

Recommended Answers

All 2 Replies

At line 11 you are not creating a new PriorityQueue hence the stack overflow. It should be:

PriorityQueue<Node> trees = new PriorityQueue<Node>;

Hope that works :)

Stack overflows are almost always caused by too many recursive calls.
In this case your Node constructor includes a new Node(work, grr) which calls the constructor, which includes a new Node(work, grr) which calls the constructor, which includes a new Node(work, grr) which calls the constructor, which includes a new Node(work, grr) which calls the constructor, which ...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.