error with static in main method?

throwing an error regarding main method

Page 1 of 1

10 Replies - 4281 Views - Last Post: 03 November 2009 - 11:01 PM Rate Topic: -----

#1 zanyrogue  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 16-January 09

error with static in main method?

Post icon  Posted 02 November 2009 - 02:39 PM

Hi all,

I have staired and staired at this but I can't see why it's throwing an error regarding static in the main method.

At this point }
public static void main(String[]args) {
new Bourreau();
}

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

The method main cannot be declared static; static methods can only be declared in a static or top level type

Could anyone please show me where i'm going wrong before I throw myself and my pc out of a window! :crazy:


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

public class Bourreau extends JFrame implements ActionListener {
	public static final long serialVersionUID = 1L;

	// Variables

	JFrame window = null;
	JTextField Box = new JTextField(20);
	// JButton Clic = new JButton("Clic");
	JLabel labelGuess;
	JPanel panelGuess;
	JPanel panelIn;
	Bourreau2 theBourreau;

	String[] words = { "cheese", "onion", "beach", "ball", "sun" }; // Array of
																	// words to
																	// guess
	String word;
	static StringBuffer guess = new StringBuffer();
	int random;

	static boolean found = false;
	static int Counter = 0; // Counts the turns taken

	// Word and string length loop

	public Bourreau() {
		Random r = new Random(); // Assigns integer r to random
		random = r.nextInt(words.length); // Assigns the value of random
		word = words[random]; // String word chooses words at random
		// System.out.println(word);

		for (int i = word.length(); i > 0; i--) { // For loop that checks string
													// word length add the
													// correct number of dashes
			guess.append('-');
		}
		// Clic.addActionListener(this);
		theBourreau = new Bourreau2();
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setTitle("Bourreau");

		labelGuess = new JLabel("", JLabel.CENTER);
		panelGuess = new JPanel();
		panelIn = new JPanel();
		panelGuess.add(labelGuess);
		labelGuess.setText(guess.toString());
		panelIn.add(Box);

		add(theBourreau, BorderLayout.NORTH);
		// add(Clic, BorderLayout.SOUTH);
		// add(panelGuess, BorderLayout.CENTER);
		add(panelIn, BorderLayout.SOUTH);
		pack();
		setVisible(true);
		/*
		 * JOptionPane .showMessageDialog( window,(
		 * "Please enter a letter and press enter, continue until you solve the word.\n\nIf the letter is in the word a position on the line will be taken.\n\n"
		 * )); window.setVisible(false);
		 */

		KeyListener tap = new KeyListener() { // Allows keyboard input

			public void keyPressed(KeyEvent e) { // Listens to the keyboard
													// input
				if (e.getKeyCode() == 10) {
					actionPerformed(null);
					// theBourreau.draw();
				}
			}

			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
			}

			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
			
			}
			public static void main(String[]args) {
				new Bourreau();
			}

			public void actionPerformed(ActionEvent e) {
				if (e == null) {
					// if (e.getSource() == Clic)
				}

				char s = Box.getText().charAt(0); // Text field box to get
													// character from the first
													// letter charAt(0)is the
													// first place

				for (int i = 0; i < word.length(); i++) {

					if (word.indexOf(s, i) >= 0) {
						guess.setCharAt(word.indexOf(s, i), s);

					}
				}

				if (!(word.indexOf(s) >= 0)) // If the guess is not in the index
					Counter++;

				if (word.compareTo(guess.toString()) == 0)
				; // Compares the guess to the string words

				// panelGuess.repaint();
				theBourreau.draw();
			}
		};
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

	}
}



Is This A Good Question/Topic? 0
  • +

Replies To: error with static in main method?

#2 cmwise  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 169
  • Joined: 14-February 09

Re: error with static in main method?

Posted 02 November 2009 - 02:45 PM

Not a java expert, but have you tried putting a space in between String[] and args in your parameter list?
Was This Post Helpful? 0
  • +
  • -

#3 zanyrogue  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 16-January 09

Re: error with static in main method?

Posted 02 November 2009 - 02:48 PM

View Postcmwise, on 2 Nov, 2009 - 01:45 PM, said:

Not a java expert, but have you tried putting a space in between String[] and args in your parameter list?


Thanks. Yes I had already just in case but it doesn't make a difference.
Was This Post Helpful? 0
  • +
  • -

#4 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1011
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: error with static in main method?

Posted 02 November 2009 - 02:54 PM

hi
is that the code you use?
public static void main(String[]args) {
				new Bourreau();
			}



if you want a new object of Boureou, you should declare it as:
public static void main(String[]args) {
Boureau b = new Bourreau();
//as you would declare any object..
Random r = new Random(); //an example from your code.
}


Was This Post Helpful? 0
  • +
  • -

#5 zanyrogue  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 16-January 09

Re: error with static in main method?

Posted 02 November 2009 - 02:59 PM

View Postjapanir, on 2 Nov, 2009 - 01:54 PM, said:

hi
is that the code you use?
public static void main(String[]args) {
				new Bourreau();
			}



if you want a new object of Boureou, you should declare it as:
public static void main(String[]args) {
Boureau b = new Bourreau();
//as you would declare any object..
Random r = new Random(); //an example from your code.
}



Ah yes thank you. You may have solved it! :^:
Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: error with static in main method?

Posted 02 November 2009 - 03:01 PM

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

...

	public Bourreau() { 
		...
		KeyListener tap = new KeyListener() { // Allows keyboard input

			...
			public static void main(String[]args) {  // Error is where this is located!
				new Bourreau();
			}

			public void actionPerformed(ActionEvent e) {
				if (e == null) {
					// if (e.getSource() == Clic)
				}

				char s = Box.getText().charAt(0); // Text field box to get
													// character from the first
													// letter charAt(0)is the
													// first place

				for (int i = 0; i < word.length(); i++) {

					if (word.indexOf(s, i) >= 0) {
						guess.setCharAt(word.indexOf(s, i), s);

					}
				}

				if (!(word.indexOf(s) >= 0)) // If the guess is not in the index
					Counter++;

				if (word.compareTo(guess.toString()) == 0)
		; // Compares the guess to the string words

				// panelGuess.repaint();
				theBourreau.draw();
			}
		};
	}



Alright the main method and the actionPerformed methods are both located in the wrong part of the code. You have them declared inside of a variable definition (KeyListener tap = new KeyListener() { ) This is defining HOW something is done (a method) not a class. To fix this, Move these out of this code block to here:

public class Bourreau extends JFrame implements ActionListener {
   ...

	public Bourreau() {
		...
		KeyListener tap = new KeyListener() { // Allows keyboard input


			// ---------_ These methods are ok because they are called only when a keylistener is called
			public void keyPressed(KeyEvent e) { // Listens to the keyboard
													// input
				if (e.getKeyCode() == 10) {
					actionPerformed(null);
					// theBourreau.draw();
				}
			}

			public void keyReleased(KeyEvent e) {
				// TODO Auto-generated method stub
			}

			public void keyTyped(KeyEvent e) {
				// TODO Auto-generated method stub
		   }

		}; /// ------------- End of Keylistener constructor
	} /// -------------- End of constructor

	public static void main(String[]args) {
		new Bourreau();
	 }

	 public void actionPerformed(ActionEvent e) {
		....
	  }
}



That's all that you need to do.

This post has been edited by Dogstopper: 02 November 2009 - 03:08 PM

Was This Post Helpful? 0
  • +
  • -

#7 zanyrogue  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 91
  • Joined: 16-January 09

Re: error with static in main method?

Posted 02 November 2009 - 03:05 PM

View Postzanyrogue, on 2 Nov, 2009 - 01:59 PM, said:

View Postjapanir, on 2 Nov, 2009 - 01:54 PM, said:

hi
is that the code you use?
public static void main(String[]args) {
				new Bourreau();
			}



if you want a new object of Boureou, you should declare it as:
public static void main(String[]args) {
Boureau b = new Bourreau();
//as you would declare any object..
Random r = new Random(); //an example from your code.
}



Ah yes thank you. You may have solved it! :^:


I was wrong it doesn't work.
Was This Post Helpful? 0
  • +
  • -

#8 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: error with static in main method?

Posted 02 November 2009 - 03:06 PM

View Postjapanir, on 2 Nov, 2009 - 03:54 PM, said:

hi
is that the code you use?
public static void main(String[]args) {
				new Bourreau();
			}



if you want a new object of Boureou, you should declare it as:
public static void main(String[]args) {
Boureau b = new Bourreau();
//as you would declare any object..
Random r = new Random(); //an example from your code.
}



Both are perfectly acceptable ways to initiate something that you do not need a reference to later (I usually use this because I do not need to use that object later):
public static void main(String[]args) {
	// If I don't need to look at this later, this works.
	 new Bourreau();
}

// Sometimes, though I do...
public static void main(String[]args) {
	 Bourreau obj = new Bourreau();
	 obj.doSomething()
}



Both work!
Was This Post Helpful? 0
  • +
  • -

#9 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: error with static in main method?

Posted 02 November 2009 - 07:40 PM

Your main() method is NOT in your class Bourreau but inside the inner class

KeyListener tap = new KeyListener() { // Allows keyboard input
.....
public static void main(....


remove it from there and put it before your declaration of a new KeyListener()
Was This Post Helpful? 0
  • +
  • -

#10 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: error with static in main method?

Posted 03 November 2009 - 09:37 PM

View Postpbl, on 2 Nov, 2009 - 08:40 PM, said:

Your main() method is NOT in your class Bourreau but inside the inner class

KeyListener tap = new KeyListener() { // Allows keyboard input
.....
public static void main(....


remove it from there and put it before your declaration of a new KeyListener()


I thought I said that... Oh well! Yay for simplicity! :P
Was This Post Helpful? 1
  • +
  • -

#11 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8343
  • View blog
  • Posts: 31,890
  • Joined: 06-March 08

Re: error with static in main method?

Posted 03 November 2009 - 11:01 PM

View PostDogstopper, on 3 Nov, 2009 - 08:37 PM, said:

View Postpbl, on 2 Nov, 2009 - 08:40 PM, said:

Your main() method is NOT in your class Bourreau but inside the inner class

KeyListener tap = new KeyListener() { // Allows keyboard input
.....
public static void main(....


remove it from there and put it before your declaration of a new KeyListener()


I thought I said that... Oh well! Yay for simplicity! :P

Sorry... I'll click your "This post was Helpful" for that
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1