0

I wrote the following code:

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);
    } //more code here...
}

I keep getting the following error: Exception in thread "main" java.lang.NoSuchMethodError: main

I've tried to add an additional main (String[] args) to the code but I still get the same errors.

What additionally I should put in the code and where in the layout should it go?

Paŭlo Ebermann
  • 68,531
  • 18
  • 138
  • 203
Craig Pottinger
  • 125
  • 1
  • 2
  • 7

2 Answers2

4

Make sure your main method looks like this:

public static void main(String[] args) {
   ...
}
NPE
  • 438,426
  • 93
  • 887
  • 970
3

Beginners with Java and Swing should check out the Oracle tutorial. Here's their Swing Hello World program:

package start;

import javax.swing.*;        

public class HelloWorldSwing {
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add the ubiquitous "Hello World" label.
        JLabel label = new JLabel("Hello World");
        frame.getContentPane().add(label);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }});}}
Nathan Hughes
  • 85,411
  • 19
  • 161
  • 250