0

While I was learning a book, I came across a Java example. When I compiled it, I had no error. But when I ran it , it showed:

Exception in thread "main" java.lang.NoSuchMethodError: main

Since I am beginner in Java, I don't know what it is.

The program is:


import java.awt.*;
import java.awt.event.*;

class Party
{
public void buildInvite(){
    Frame f = new Frame();
    Label l = new Label("Party at Tim's");

    Button B = new Button("You Bet");
    Button c = new Button("Shoot me");
    Panel p = new Panel();
    p.add(l);
}
}
Andrew Thompson
  • 163,965
  • 36
  • 203
  • 405
Pari Sairam Mohan
  • 391
  • 1
  • 2
  • 10

3 Answers3

2

If you still want to see the execution of this code try this version:

class Party{
        public void buildInvite(){
        Frame f = new Frame();
        Label l = new Label("Party at Tim's");
        Button B = new Button("You Bet");
        Button c = new Button("Shoot me");
        Panel p = new Panel();
        p.add(l);
        p.add(B);
        p.add(c);
        f.add(p);
        f.setVisible(true);
        }

        public static void main(String[] args) {
            new Party().buildInvite();
        }
    }
Lynch
  • 8,224
  • 2
  • 21
  • 33
1

This code can't be fixed with less work than a complete rewrite. It doesn't have a main method, cause the frame to show or add the panel to it. Find a better tutorial.

Mike Samuel
  • 109,453
  • 27
  • 204
  • 234
1

All Java programs must have a main method. This is what the JVM looks for to start your program.

Ash Burlaczenko
  • 21,581
  • 14
  • 63
  • 95