1

Possible Duplicate:
Causes of 'java.lang.NoSuchMethodError: main Exception in thread “main”'

I'm getting the following error:

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

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;

public class SwimCalc extends JFrame implements ActionListener {
   private JTabbedPane jtabbedPane;
   private JPanel Customers;

   JTextArea NameTextCustomers, ExistTextCustomers, NameTextContractors,
         ExistTextContractors;

   public SwimCalc() {
      setTitle("Volume Calculator");
      setSize(300, 200);

      JPanel topPanel = new JPanel();
      topPanel.setLayout(new BorderLayout());
      getContentPane().add(topPanel);

      createCustomers();

      jtabbedPane = new JTabbedPane();
      jtabbedPane.addTab("Customers", Customers);
      topPanel.add(jtabbedPane, BorderLayout.CENTER);
   }

   /* CREATE CUSTOMERS */

   public JPanel createCustomers() {
      Customers = new JPanel();
      Customers.setLayout(null);

      NameTextCustomers = new JTextArea();
      NameTextCustomers.setBounds(10, 10, 350, 150);
      NameTextCustomers.setLineWrap(true);
      Customers.add(NameTextCustomers);

      JButton Exit = new JButton("Exit");
      Exit.setBounds(30, 170, 80, 20);
      Exit.addActionListener(this);
      Exit.setBackground(Color.white);
      Customers.add(Exit);

      JButton AddCustomers = new JButton("Add Customer");
      AddCustomers.setBounds(130, 170, 120, 20);
      AddCustomers.setBackground(Color.white);
      Customers.add(AddCustomers);

      JButton Refresh = new JButton("Refresh");
      Refresh.setBounds(260, 170, 80, 20);
      Refresh.setBackground(Color.white);
      Customers.add(Refresh);

      ExistTextCustomers = new JTextArea();
      ExistTextCustomers.setBounds(10, 200, 350, 60);
      ExistTextCustomers.setLineWrap(true);
      Customers.add(ExistTextCustomers);

      final JTextArea custArea = new JTextArea(6, 30);
      final JTextArea custMessage = null;

      AddCustomers.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            new Customer("Customer");
         }
      });
      Customers.add(custArea);
      Customers.add(AddCustomers);

      Customers.add(Refresh);
      Customers.add(custMessage);
      Refresh.setMnemonic('R');

      Refresh.addActionListener(new ActionListener() {
         public void actionPerformed(ActionEvent e) {
            custMessage.setText("");
            try {
               File custOpen = new File("customer.txt");
               FileReader custAreaIn = new FileReader(custOpen);
               custArea.read(custAreaIn, custOpen.toString());
               custMessage.setText("The file exists and can be read from.");
            } catch (IOException e3) {
               custMessage.setText("The file could not be read. "
                     + e3.getMessage());
            }
         }
      });
      return Customers;
   }

   class Customer extends JFrame {
      private String[] states = { "AL", "AK", "AZ", "AR", "CA", "CO", "CT",
            "DE", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA",
            "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH",
            "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC",
            "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY" };
      private JComboBox StateList = new JComboBox(states);
      private JTextField NameText = new JTextField(25);
      private JTextField AddressText = new JTextField(25);
      private JTextField CityText = new JTextField(25);
      private JTextField ZipText = new JTextField(9);
      private JTextField PhoneText = new JTextField(10);
      private JTextField PopMessageText = new JTextField(30);
      private static final long serialVersionUID = 1L;

      private AddCustButtonHandler addCusHandler = new AddCustButtonHandler();

      public Customer(String who) {
         popUpWindow(who);
      }

      public void popUpWindow(final String who) {

         final JFrame popWindow;
         popWindow = new JFrame(who);
         popWindow.setSize(425, 350);
         popWindow.setLocation(100, 100);
         popWindow.setVisible(true);
         setDefaultCloseOperation(EXIT_ON_CLOSE);

         Container c = new Container();

         popWindow.add(c);

         c.setLayout(new FlowLayout());

         JPanel one = new JPanel();
         JPanel two = new JPanel();
         JPanel three = new JPanel();
         JPanel four = new JPanel();
         JPanel five = new JPanel();
         JPanel six = new JPanel();

         one.add(new JLabel(who + " Name "));
         one.add(NameText);
         two.add(new JLabel("Address "));
         two.add(AddressText);
         three.add(new JLabel("City "));
         three.add(CityText);
         four.add(new JLabel("State "));
         StateList.setSelectedIndex(0);
         four.add(StateList);
         four.add(new JLabel("ZIP"));
         four.add(ZipText);
         four.add(new JLabel("Phone"));
         four.add(PhoneText);
         JButton addwho = new JButton("Add " + who);
         addwho.setMnemonic('A');
         JButton close = new JButton("Close");
         close.setMnemonic('C');
         JButton deleteFile = new JButton("Delete File");
         deleteFile.setMnemonic('D');
         five.add(addwho);
         five.add(close);
         five.add(deleteFile);
         PopMessageText.setEditable(false);
         PopMessageText.setHorizontalAlignment(JTextField.CENTER);

         six.add(PopMessageText);
         c.add(one);
         c.add(two);
         c.add(three);
         c.add(four);
         c.add(five);
         c.add(six);

         deleteFile.setToolTipText("Delete File");
         addwho.setToolTipText("Add " + who);
         close.setToolTipText("Close");

         if (who == "Customer")
            addwho.addActionListener(addCusHandler);
         close.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               NameText.setText("");
               AddressText.setText("");
               CityText.setText("");
               ZipText.setText("");
               PhoneText.setText("");
               PopMessageText.setText("");
               popWindow.dispose();
            }
         });
         deleteFile.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
               PopMessageText.setText("");
               if (who == "Customer") {
                  File file = new File("Customer.txt");
                  boolean cusFileDeleted = file.delete();
                  if (cusFileDeleted) {
                     PopMessageText.setText("Customer file has been deleted");
                  } else {
                     PopMessageText
                           .setText("There was an erron in deleting file");
                  }
               }
            }
         });
      }

      class AddCustButtonHandler implements ActionListener {
         public void actionPerformed(ActionEvent addCusHandler) {
            int StateIndex;
            try {
               File file = new File("Customer.txt");

               boolean success = file.createNewFile();

               if (success) {
                  PopMessageText
                        .setText("Customer.txt file created file added");
               } else if (file.canWrite()) {
                  PopMessageText
                        .setText("Writing data to Customer.txt, file added");
               } else {
                  PopMessageText.setText("Cannot create file: Customer.txt");
               }
               try {
                  FileWriter fileW = new FileWriter("Customer.txt", true);
                  fileW.write(NameText.getText());
                  fileW.write(",");
                  fileW.write(AddressText.getText());
                  fileW.write(",");
                  fileW.write(CityText.getText());
                  fileW.write(",");
                  StateIndex = StateList.getSelectedIndex();
                  fileW.write(states[StateIndex]);
                  fileW.write(",");
                  fileW.write(ZipText.getText());
                  fileW.write(",");
                  fileW.write(PhoneText.getText());
                  fileW.write("\r\n");
                  fileW.close();
                  PopMessageText.setText("A new Customer has been added!");

                  FileReader fileR = new FileReader("Customer.txt");
                  BufferedReader buffIn = new BufferedReader(fileR);

                  String textData = buffIn.readLine();
                  buffIn.close();
               } catch (IOException e1) {
                  JOptionPane.showMessageDialog(null, e1.getMessage(), "ERROR",
                        2);
               }
               NameText.setText("");
               AddressText.setText("");
               CityText.setText("");
               ZipText.setText("");
               PhoneText.setText("");
            } catch (IOException e1) {
            }

         }
      }

      public void actionPerformed(ActionEvent event) {
      }

      private void Exit_pressed() {
         System.exit(0);
      }

      public void main(String[] args) {
         JFrame frame = new SwimCalc();
         frame.setSize(380, 350);
         frame.setVisible(true);
      }
   }

   public void actionPerformed(ActionEvent e) {

   }
}

The error:

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

public void actionPerformed(ActionEvent e) {

    }
    public static void main(String[] args){
        JFrame frame = new SwimCalc();
        frame.setSize(380, 350);
        frame.setVisible(true);
        }
    }
Community
  • 1
  • 1
Mike
  • 2,163
  • 13
  • 41
  • 54
  • 3
    Not at all a duplicate of that question. Extremely common error messages like this can have varying causes, and pointing a newbie to a question that addresses a completely different cause isn't very constructive. – Mark Peters Mar 23 '11 at 14:42
  • All of the code I copied above is what I'm running. When I cahnge it to static I get this error: Exception in thread "main" java.lang.Error: Unresolved compilation problem: The method main cannot be declared static; static methods can only be declared in a static or top level type at SwimCalc$Customer.main(SwimCalc.java:267) – Mike Mar 23 '11 at 14:53
  • @Mike: see [my answer](http://stackoverflow.com/questions/5406861/java-lang-nosuchmethoderror-main/5406869#5406869). – Mark Peters Mar 23 '11 at 14:59
  • @Mark: I moved it to with the last bracket, still no luck if that's where it needs to be. – Mike Mar 23 '11 at 15:04
  • @Mike: did you also make it static? If it's still not working edit your question with an update showing what you've tried and what error message you're getting now. – Mark Peters Mar 23 '11 at 15:06
  • @Mark: How do you copy code in the comments? Or do you want me to adjust my first post above? Thanks – Mike Mar 23 '11 at 15:09
  • @Mike: Edit your post above. You should add it to the end to preserve your original text. – Mark Peters Mar 23 '11 at 15:11
  • @Mark: I added it to the bottom. – Mike Mar 23 '11 at 15:16
  • Why does nobody even READ COMPILER MESSAGES ANYMORE? – Bombe Mar 23 '11 at 15:22
  • @Mike: Are you sure you recompiled your class? Did you follow my instructions in the answer for invoking your program (using `java SwimCalc`)? – Mark Peters Mar 23 '11 at 15:28
  • @Bombe: Take it easy. The error message implies that there is no method named "main". That is incredibly difficult for a newbie to figure out when they get that error message twice, and in both cases **they have defined a method named "main"**. The error message does not at ALL directly say what the problem is. – Mark Peters Mar 23 '11 at 15:29
  • @Mark: I just noticed if I go to the top of Eclipse and click the drop down box next to run and choose as run java application, it shows new errors. Exception in thread "main" java.lang.NullPointerException at java.awt.Container.addImpl(Unknown Source) at java.awt.Container.add(Unknown Source) at SwimCalc.createCustomers(SwimCalc.java:74) at SwimCalc.(SwimCalc.java:21) at SwimCalc.main(SwimCalc.java:273) – Mike Mar 23 '11 at 15:35
  • 1
    See http://stackoverflow.com/questions/5407250/causes-of-java-lang-nosuchmethoderror-main-exception-in-thread-main for a generic answer – Stephen C Mar 23 '11 at 15:35
  • @Mike: then that means it's finding your main method. If you have more problems I think a new question is in order...there are a lot of issues with this code; more than we can address in one question. But try to diligently research it on your own before opening a new question. – Mark Peters Mar 23 '11 at 15:43

8 Answers8

7

main needs to be a static method.

public static void main(String[] args){
   JFrame frame = new SwimCalc();
   frame.setSize(380, 350);
   frame.setVisible(true);
}

Also, the main method should be in the SwimCalc class or some other top-level class. Move the method to be within the SwimCalc class (not in your Customer class) and use java SwimCalc to invoke it. You can't declare main in an inner class.

Mark Peters
  • 76,122
  • 14
  • 153
  • 186
2

Yes the signature should be

public static void main(String[] args)

Even if the args is missed eclipse throws this error.

andrewsi
  • 10,954
  • 132
  • 33
  • 48
kinns
  • 21
  • 1
2

It looks your main method is actually inside your actionPerformed method. You obviously cannot do this. The declaration needs to be in your SwimCalc class. Is that craziness your actual code that you are trying to run? You've got braces and stray parentheses all over the place, no indentation; if you just clean up your code the problem will be a lot easier to find.

and

public void main(String[] args){

should be

public static void main(String[] args){

There's also nonsense like this: } );. You have so many syntax errors...

Travis Webb
  • 13,507
  • 6
  • 51
  • 101
  • Lol yeah I thought so too until I found a closing brace hidden all the way to the right of the code area. I updated the question to format the code properly. – Mark Peters Mar 23 '11 at 15:01
  • oh nice work. yea I don't have the mod points to be able to do that – Travis Webb Mar 23 '11 at 15:31
0
public **static** void main(String[] args){
Rick
  • 3,745
  • 1
  • 15
  • 16
0

Main method must be public static

public static void main(String[] args)
sudocode
  • 15,747
  • 38
  • 58
0

main() must be static. The complete signature is:

public static void main(String[] args)
Soronthar
  • 1,611
  • 10
  • 10
  • After doing that I had the same error. One of the options when you hover over the error was to "add static modifier to parent type". I tried doing this, but had the same error as stated. – Mike Mar 23 '11 at 14:42
  • @Mike: your current code doesn't compile and it is poorly formatted. Can you give us the real code, cleanly formatted? – Mark Peters Mar 23 '11 at 14:44
  • @Mark: I know it doesn't compile. I'm still learning java (very new) so I know it isn't going to be of a professional standards. When you run it does it give the error I stated? – Mike Mar 23 '11 at 14:50
  • @Mike: If there is no "compile", there is no "run". Your class **must** compile before you can run it. However, you can see the update in my answer for another problem you might be having. – Mark Peters Mar 23 '11 at 14:53
  • Actually, it does compile, I was wrong. The formatting was so bizarre that some braces were being cropped out of the display area. – Mark Peters Mar 23 '11 at 14:55
0

It should be

public static void main(String[] args){

instead of public void main...

Roddy of the Frozen Peas
  • 11,100
  • 9
  • 37
  • 73
sharpner
  • 3,601
  • 3
  • 17
  • 27
0

You main method needs to be static. This should be the signature:

public static void main(String[] args)
Mike Deck
  • 17,069
  • 15
  • 62
  • 89