7 Replies - 374 Views - Last Post: 16 May 2014 - 05:06 AM Rate Topic: -----

#1 SilverStrike88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-January 14

Syntax Error on token "new", @ expected

Posted 12 May 2014 - 07:44 AM

So my only error in the entire code is on the 9th line of this code sample. It says "Syntax Error on token "new", @ expected" but @ does not belong there, as far as I can tell, since the error does not go away upon adding it.

 public class Obstapacalypse implements KeyListener, ActionListener {

	private static final String EXIT_ON_CLOSE = null;
	private JFrame frame;
	private Player player = new Player();
	private JPanel contentPane;
	private JButton btnStart;
	
	private Player addKeyListener(new Obstapocalypse_player_keyAdapter(this));
	private Timer absolTimer = new Timer(0, null);  //Timer creation
	int yPos = 200;
	int xPos = 600;
	private int counter;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Obstapacalypse window = new Obstapacalypse();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	} 


How do I fix this? If you would like me to attach certain files to this post for reference, let me know. Thanks a ton for all of your help!

If it makes a difference, I'm using Eclipse with jre1.7.

Is This A Good Question/Topic? 0
  • +

Replies To: Syntax Error on token "new", @ expected

#2 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 143
  • View blog
  • Posts: 569
  • Joined: 24-April 10

Re: Syntax Error on token "new", @ expected

Posted 12 May 2014 - 07:49 AM

09	   private Player addKeyListener(new Obstapocalypse_player_keyAdapter(this));
You can't do this. After private Player you need to provide a name for this refence, such as:
private Player myPlayer;

In your constructor you can add the keylistener.
public Obstapacalypse() {

   myPlayer = new Player();
   addKeyListener(new Obstapocalypse_player_keyAdapter(this));
}

This post has been edited by mike73: 12 May 2014 - 07:50 AM

Was This Post Helpful? 1
  • +
  • -

#3 SilverStrike88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-January 14

Re: Syntax Error on token "new", @ expected

Posted 12 May 2014 - 08:12 AM

View Postmike73, on 12 May 2014 - 07:49 AM, said:

09	   private Player addKeyListener(new Obstapocalypse_player_keyAdapter(this));
You can't do this. After private Player you need to provide a name for this refence, such as:
private Player myPlayer;

In your constructor you can add the keylistener.
public Obstapacalypse() {

   myPlayer = new Player();
   addKeyListener(new Obstapocalypse_player_keyAdapter(this));
}



I took your advice and added the keylistener in my constructor like this:

 public Obstapacalypse() {
		initialize();
		
		player = new Player();
		addKeyListener(new Obstapocalypse_player_keyAdapter(this));


	} 


But now it says "Obstapocalypse_player_keyAdapter cannot be resolved to a type". I took these lines out of where I originally had this problem.

Also, initialize is my method for making the window appear and all, can I not call to it here?
Was This Post Helpful? 0
  • +
  • -

#4 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 143
  • View blog
  • Posts: 569
  • Joined: 24-April 10

Re: Syntax Error on token "new", @ expected

Posted 13 May 2014 - 12:01 AM

You could do it there, but why not just use the constructor? This code:
5	       addKeyListener(new Obstapocalypse_player_keyAdapter(this));
implies that you have a class called Obstapocalypse_player_keyAdapter which either implements KeyListener or extends KeyAdapter. Since your Obstapacalypse class implements KeyListener, I think you mean to add Obstapacalypse as the KeyListener:
addKeyListener(this);

Was This Post Helpful? 0
  • +
  • -

#5 SilverStrike88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-January 14

Re: Syntax Error on token "new", @ expected

Posted 13 May 2014 - 03:13 PM

View Postmike73, on 13 May 2014 - 12:01 AM, said:

You could do it there, but why not just use the constructor? This code:
5	       addKeyListener(new Obstapocalypse_player_keyAdapter(this));
implies that you have a class called Obstapocalypse_player_keyAdapter which either implements KeyListener or extends KeyAdapter. Since your Obstapacalypse class implements KeyListener, I think you mean to add Obstapacalypse as the KeyListener:
addKeyListener(this);


I don't understand what you mean. What do I do with the constructor?
Was This Post Helpful? 0
  • +
  • -

#6 mike73  Icon User is offline

  • D.I.C Addict

Reputation: 143
  • View blog
  • Posts: 569
  • Joined: 24-April 10

Re: Syntax Error on token "new", @ expected

Posted 15 May 2014 - 01:44 AM

instead of calling a method called initialise in your constructor, why not just put the contents of this initialise method into the constructor?
Was This Post Helpful? 1
  • +
  • -

#7 astonecipher  Icon User is offline

  • Major DIC Head
  • member icon

Reputation: 575
  • View blog
  • Posts: 2,579
  • Joined: 03-December 12

Re: Syntax Error on token "new", @ expected

Posted 15 May 2014 - 02:11 AM

You instantiate a new class with a constructor.
Was This Post Helpful? 1
  • +
  • -

#8 SilverStrike88  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 29-January 14

Re: Syntax Error on token "new", @ expected

Posted 16 May 2014 - 05:06 AM

View Postmike73, on 15 May 2014 - 01:44 AM, said:

instead of calling a method called initialise in your constructor, why not just put the contents of this initialise method into the constructor?


Good idea! Thanks for all your help, I have it working now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1