Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Exception in thread \"main\" java.lang.Error: Unresolved compilation problem: No

ID: 3711153 • Letter: E

Question

Exception in thread "main" java.lang.Error: Unresolved compilation problem:

   No enclosing instance of type BinarySearchTree is accessible. Must qualify the allocation with an enclosing instance of type BinarySearchTree (e.g. x.new A() where x is an instance of BinarySearchTree).

Can someone help me with the error above.

   at BinarySearchTree.main(BinarySearchTree.java:267)

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.StringTokenizer;

public class BinarySearchTree {

   public BinarySearchTree() {

       // TODO Auto-generated constructor stub

   }

  

   public class Node {

       String key;

       int value;

       Node left;

       Node right;

       Node(String line )

       {

           // parse the string into variables

           StringTokenizer st = new StringTokenizer(line," ");

           this.key= st.nextElement().toString();

           this.value = Integer.parseInt(st.nextElement().toString());

       }

       Node(String key, int value) {

          this.key = key;

          this.value = value;

       }

       public String toString() {

           return key + " " + value;

       }

   }

   Node root;

   int MAX = 0;

   int MIN = 0;

   /***

   * Add Node to the tree

   *@param Country Name

   *@param Population

   */

   public void addNode(String key, int value) {

       // Create a new Node and initialize it

       Node newNode = new Node(key, value);

       // If no root this will become root

       if (root == null) {

           root = newNode;

       } else {

           // Set root as the Node and start traversing the tree

           Node focusNode = root;

           // Future parent for new Node

           Node parent;

           while (true) {

               // root is the top parent

               parent = focusNode;

               // Check if the new node should go on the left of parent

               if (key.compareToIgnoreCase(focusNode.key) < 0) {

                   // focus to the left child

                   focusNode = focusNode.left;

                   // If the left child has no children

                   if (focusNode == null) {

                       // then place the new node on the left of it

                       parent.left = newNode;

                       return;

                   }

               } else {

                   // If we get here put the node on the right

                   focusNode = focusNode.right;

                   // If the right child has no children

                   if (focusNode == null) {

                       // then place the new node on the right of it

                       parent.right = newNode;

                       return;

                   }

               }

           }

       }

   }

   /***

   * Order the tree ascending by key

   *@param node

   *@return

   */

   public Node orderTreeByCountry(Node node)

   {

       if(node != null)

       {

           orderTreeByCountry(node.left);

           System.out.println(node);

           orderTreeByCountry(node.right);

       }

       return node;

   }

   /***

   * Find node by providing key name

   *@param Node

   *@return Node

   */

   public Node findNode(String key) {

       // Start at the top of the tree

       Node focusNode = root;

       // While node not found keep looking

       while (focusNode.key.compareToIgnoreCase(key) != 0) {

           // If we should search to the left

           if (key.compareToIgnoreCase(focusNode.key) < 0) {

               // Shift the focus Node to the left child

               focusNode = focusNode.left;

           } else {

               // Shift the focus Node to the right child

               focusNode = focusNode.right;

           }

           // The node wasn't found

           if (focusNode == null)

               return null;

       }

       return focusNode;

   }

   /***

   * Get the value of node with specific key

   *@param key

   *@return Value

   */

   public int getValueForKey(String key)

   {

       // Start at the top of the tree

               Node focusNode = root;

               // While node not found

               while (focusNode.key.compareToIgnoreCase(key) != 0) {

                   // If we should search to the left

                   if (key.compareToIgnoreCase(focusNode.key) < 0) {

                       // Shift the focus Node to the left child

                       focusNode = focusNode.left;

                   } else {

                       // Shift the focus Node to the right child

                       focusNode = focusNode.right;

                   }

                   // The node wasn't found

                   if (focusNode == null)

                       return -1;

               }

               return focusNode.value;

   }

   /***

   * Get how many nodes in the tree

   *@param node

   *@return number of nodes

   */

   public int getNodesCount(Node node)

   {

       if(node == null) {

           return 0;

       } else {

           int count = 1;

           count += getNodesCount(node.left);

           count += getNodesCount(node.right);

           return count;

       }

   }

   /***

   * Get min value for tree which is based on key, value attributes

   *@param node

   *@return min value

   */

   public int getMinValue(Node node)

   {  

       if(node != null)

       {  

           getMinValue(node.left);  

           if(MIN == 0 ) {

               MIN = node.value;

           }

           if(MIN > node.value) {

               MIN = node.value;

           }

           getMinValue(node.right);

       }

       return MIN;

   }

   /***

   * Get max value for tree which is based on key, value attributes

   *@param node

   *@return max value

   */

   public int getMaxValue(Node node)

   {

       if(node != null)

       {  

           getMaxValue(node.left);  

           if(MAX == 0 ) {

               MAX = node.value;

           }

           if(MAX < node.value) {

               MAX = node.value;

           }

           getMaxValue(node.right);

       }

       return MAX;

   }

   public static void main(String[] args)

   {

       BufferedReader br;

       BinarySearchTree btree = new BinarySearchTree();

       try {

           br = new BufferedReader(new FileReader("numbers.txt"));

           String line = "";

           while ((line = br.readLine()) != null) {

                 /*Create Country node from the line */

             Node country = new Node(line);

             btree.addNode(country.key, country.value);    

          }

       br.close();

       }

       catch (FileNotFoundException e) {

           System.out.println(e.getLocalizedMessage());

       }

       catch (IOException e) {

           System.out.println(e.getLocalizedMessage());

       }

btree.orderTreeByCountry(btree.root);

System.out.println("Number of Countries is : " + btree.getNodesCount(btree.root));

System.out.println("Min Population is : " + btree.getMinValue(btree.root));

System.out.println("Max Population is : " + btree.getMaxValue(btree.root));

int population = btree.getValueForKey("xxx");

   if(population== -1)

   System.out.println("Sorry this Country is not in this btree");

else

   System.out.println("Population is : " + population);

   }

}

Explanation / Answer

This error is coming because Node is inner class of BinarySearchTree class and we are trying to instantiate it without the class reference. So to resolve this error, we need to make the inner class static. I am posting the code below. Please let me know in comments if you still find same issue....

Bold is the line which I have changed.

public class BinarySearchTree {

public BinarySearchTree() {

// TODO Auto-generated constructor stub

}

public static class Node {

String key;

int value;

Node left;

Node right;

Node(String line)

{

// parse the string into variables

StringTokenizer st = new StringTokenizer(line, " ");

this.key = st.nextElement().toString();

this.value = Integer.parseInt(st.nextElement().toString());

}

Node(String key, int value) {

this.key = key;

this.value = value;

}

public String toString() {

return key + " " + value;

}

}

Node root;

int MAX = 0;

int MIN = 0;

/***

*

* Add Node to the tree

*

* @param Country

* Name

*

* @param Population

*

*/

public void addNode(String key, int value) {

// Create a new Node and initialize it

Node newNode = new Node(key, value);

// If no root this will become root

if (root == null) {

root = newNode;

} else {

// Set root as the Node and start traversing the tree

Node focusNode = root;

// Future parent for new Node

Node parent;

while (true) {

// root is the top parent

parent = focusNode;

// Check if the new node should go on the left of parent

if (key.compareToIgnoreCase(focusNode.key) < 0) {

// focus to the left child

focusNode = focusNode.left;

// If the left child has no children

if (focusNode == null) {

// then place the new node on the left of it

parent.left = newNode;

return;

}

} else {

// If we get here put the node on the right

focusNode = focusNode.right;

// If the right child has no children

if (focusNode == null) {

// then place the new node on the right of it

parent.right = newNode;

return;

}

}

}

}

}

/***

*

* Order the tree ascending by key

*

* @param node

*

* @return

*

*/

public Node orderTreeByCountry(Node node)

{

if (node != null)

{

orderTreeByCountry(node.left);

System.out.println(node);

orderTreeByCountry(node.right);

}

return node;

}

/***

*

* Find node by providing key name

*

* @param Node

*

* @return Node

*

*/

public Node findNode(String key) {

// Start at the top of the tree

Node focusNode = root;

// While node not found keep looking

while (focusNode.key.compareToIgnoreCase(key) != 0) {

// If we should search to the left

if (key.compareToIgnoreCase(focusNode.key) < 0) {

// Shift the focus Node to the left child

focusNode = focusNode.left;

} else {

// Shift the focus Node to the right child

focusNode = focusNode.right;

}

// The node wasn't found

if (focusNode == null)

return null;

}

return focusNode;

}

/***

*

* Get the value of node with specific key

*

* @param key

*

* @return Value

*

*/

public int getValueForKey(String key)

{

// Start at the top of the tree

Node focusNode = root;

// While node not found

while (focusNode.key.compareToIgnoreCase(key) != 0) {

// If we should search to the left

if (key.compareToIgnoreCase(focusNode.key) < 0) {

// Shift the focus Node to the left child

focusNode = focusNode.left;

} else {

// Shift the focus Node to the right child

focusNode = focusNode.right;

}

// The node wasn't found

if (focusNode == null)

return -1;

}

return focusNode.value;

}

/***

*

* Get how many nodes in the tree

*

* @param node

*

* @return number of nodes

*

*/

public int getNodesCount(Node node)

{

if (node == null) {

return 0;

} else {

int count = 1;

count += getNodesCount(node.left);

count += getNodesCount(node.right);

return count;

}

}

/***

*

* Get min value for tree which is based on key, value attributes

*

* @param node

*

* @return min value

*

*/

public int getMinValue(Node node)

{

if (node != null)

{

getMinValue(node.left);

if (MIN == 0) {

MIN = node.value;

}

if (MIN > node.value) {

MIN = node.value;

}

getMinValue(node.right);

}

return MIN;

}

/***

*

* Get max value for tree which is based on key, value attributes

*

* @param node

*

* @return max value

*

*/

public int getMaxValue(Node node)

{

if (node != null)

{

getMaxValue(node.left);

if (MAX == 0) {

MAX = node.value;

}

if (MAX < node.value) {

MAX = node.value;

}

getMaxValue(node.right);

}

return MAX;

}

public static void main(String[] args)

{

BufferedReader br;

BinarySearchTree btree = new BinarySearchTree();

try {

br = new BufferedReader(new FileReader("numbers.txt"));

String line = "";

while ((line = br.readLine()) != null) {

/* Create Country node from the line */

Node country = new Node(line);

btree.addNode(country.key, country.value);

}

br.close();

}

catch (FileNotFoundException e) {

System.out.println(e.getLocalizedMessage());

}

catch (IOException e) {

System.out.println(e.getLocalizedMessage());

}

btree.orderTreeByCountry(btree.root);

System.out.println("Number of Countries is : " + btree.getNodesCount(btree.root));

System.out.println("Min Population is : " + btree.getMinValue(btree.root));

System.out.println("Max Population is : " + btree.getMaxValue(btree.root));

int population = btree.getValueForKey("xxx");

if (population == -1)

System.out.println("Sorry this Country is not in this btree");

else

System.out.println("Population is : " + population);

}

}