简体   繁体   中英

Error - ''Main method not found in class Node, please define the main method as…" in the following code:

Exact error: "Error: Main method not found in class Node, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application"

class Node{
    int key;
    Node left, right;

    public Node(int item){
        key = item;
        left = right = null;
    }
}

class BinaryTree{
    Node root;

    BinaryTree(){
        root = null;
    }

    void printPostorder(Node node){
        if(node == null)
            return;
        
        printPostorder(node.left);
        printPostorder(node.right);
        System.out.print(node.key + " ");
    }

    void printPostorder(){ printPostorder(root);}

    public static void main(String[] args){
        BinaryTree tree = new BinaryTree();
        tree.root = new Node(1);
        tree.root.left = new Node(2);
        tree.root.right = new Node(3);

        System.out.println("\nPostorder: ");
        tree.printPostorder();
    }
}

But the main function has been defined.

I suspect you've named the java file as "Node.java" instead of "BinaryTree.java", the code throws error since there isn't any main function in class Node (that you're trying to run) but in class BinaryTree . The problem shall resolve if you rename your file to BinaryTree.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM