Zach
Zach

Reputation: 5083

Getting a weird java.lang.NoSuchMethodError for my classes

I am trying to implement TreeNodes and BSTs and I have written two files to start me off. However, even at this point I am getting a really weird error. At runtime I am being told a method that clearly exists doesn't exist.

Here are both of the classes for your reference.

BST.java

public class BST {

    TreeNode root;

    public BST(int data) {
        this.root = new TreeNode(data);
    }

    public boolean insert(int data) {
        TreeNode newNode = new TreeNode(data);
        TreeNode curr = root;
        boolean inTree = false;

        while (!inTree) {

            // newNode is <= curr, place in left
            if (newNode.getValue() <= curr.getValue()) {

                if (curr.getLeft() != null) {
                    curr = curr.getLeft();
                } else {
                    curr.setLeft(newNode);
                    inTree = true;
                    System.out.println(data+" INSERTED LEFT OF "+curr);
                }

            } else {

                if (curr.getRight() != null) {
                    curr = curr.getRight();
                } else {
                    curr.setRight(newNode);
                    System.out.println(data+" INSERTED Right OF "+curr);
                    inTree = true;
                }
            }
        }
        return true;
    }

}

Main.java

public class Main {

    public static void main(String[] args) {

        BST bsTree = new BST(4);

        bsTree.insert(1);
        bsTree.insert(4);
        bsTree.insert(0);
    }
}

Running javac Main.java is fine but when I run java Main I get this error:

Exception in thread "main" java.lang.NoSuchMethodError: BST.<init>(I)V
at Main.main(Main.java:5) 

Can someone please help me understand why this is happening? Thanks

[EDIT]: Even MORE weird stuff happening.

Let's say I make a completely new folder in a different directory and put this file:

public class BST {

    TreeNode root;

    public BST(int val) {
        TreeNode r = new TreeNode(val);
        root = r;
    }

    public static void main(String[] args) {
        System.out.println("hey");
     }
}

Compiling it works but running it just gives me no results and doesn't terminate. However, if I rename the class to BSTe and change the file name accordingly it works...what is going on.

Upvotes: 1

Views: 627

Answers (3)

NightShade
NightShade

Reputation: 276

This code should work fine. Make sure that you have cleaned your project in your IDE. More specifically make sure that the class BST is compiled and updated so that the latest changes areseen by the Main class.

You can compile all classes at once using javac *.java

Upvotes: 1

Jens
Jens

Reputation: 69460

You have to add the compiled class BST in your classpath.

Try:

 Java -cp ./ Main

Upvotes: 2

Ren&#233; Winkler
Ren&#233; Winkler

Reputation: 7088

Try using javac *.java. This will compile all your class in the current directory.

Upvotes: 1

Related Questions