-2

I'm trying to re-learn how to code in java for a job and I've been working on a binary search tree. I've been getting this error below with the following code. Does anyone know why I've been getting this? I tried declaring an object of BinaryTree with BinaryTree binaryTree = new BinaryTree(); in the menu method and then calling the insert method as binaryTree.Insert(Integer.parseInt(keyValue), nameValue); but that didn't work either. I've also tried to use other stack overflow articles but they haven't helped me understand.

Does anyone have any insight?

java:37: error: non-static method Insert(int,String) cannot be referenced from a static context BinaryTree.Insert(Integer.parseInt(keyValue), nameValue);

import java.util.Scanner;



public class PracticeProblem2
{
    
    
    public static void main(String[] args)
    {
        
        menu();
        
        System.out.println("Thank you for using this program!");
    }
    
    public static void menu()
    {
        int x = 0;
        while(x == 0)
        {
            
            Scanner scanman = new Scanner(System.in);
            
            
            System.out.print("Please enter key value: ");
            String keyValue = scanman.nextLine();
            

            System.out.print("Please enter their name: ");
            String nameValue = scanman.nextLine();
            
            
            BinaryTree.Insert(Integer.parseInt(keyValue), nameValue);

            
            System.out.println("");
            System.out.print("Would you like to add another person (y/n)? ");
            String answer = scanman.nextLine();
            
            if(answer.equals("n"))
            {
                x = 1;
            }
            
        }
    }
}
class BinaryTree
{
    Node root;
    
    public void Insert(int setValue, String setName)
    {
        
        Node newNode = new Node(setValue, setName);
        
        //Checks if root exists. If there is no root, it sets current node as root
        if(root == null)
        {
            root = newNode;
        }
        
        //Else statement puts new node on either right side of tree or left depending on key value.
        else
        {
            
            //Creates pointer to navigate tree
            Node focusNode = root;
            
            //Creates pointer named "parent" 
            Node parent;
            
            //Creates infinite loop to navigate tree until completion.
            while(true)
            {
                parent = focusNode;
                
                
                if(setValue < focusNode.value)
                {
                    focusNode = focusNode.left;
                }
                if(focusNode == null)
                {
                    parent.left = newNode;
                    return;
                }
                else
                {
                    focusNode = focusNode.right;
                
                
                    if(focusNode == null)
                    {
                        parent.right = newNode;
                        return;
                    }
                }
            }
            
        }
        
    }   
}
class Node
{
    int value;
    String name;
    
    
    Node left;
    Node right;
    
    public Node(int setValue, String setName)
    {
        this.value = setValue;
        this.name = setName;
    }
    
    public String toString()
    {
        return name + "has a value of " + Integer.toString(value) + "\n";
    }
}
R tay
  • 1
  • 1
    Make a habit of searching for an answer before posting questions. You are unlikely to be the first person ever to run into a particular issue. – MarsAtomic Jun 26 '20 at 18:39
  • 1
    Does this answer your question? ["Non-static method cannot be referenced from a static context" error](https://stackoverflow.com/questions/4922145/non-static-method-cannot-be-referenced-from-a-static-context-error) – Roshin Raphel Jun 26 '20 at 18:40

1 Answers1

0

BinaryTree is not written as static class, so you can't use the method as BinaryTree.Insert. You'll have to create an instance of BinaryTree and call Insert from the instance:

BinaryTree binaryTree = new BinaryTree();
...
binaryTree.Insert(Integer.parseInt(keyValue), nameValue);
Soulis
  • 124
  • 1
  • 9