Peter Dempsey
Peter Dempsey

Reputation: 495

Compile Error: illegal start of expression

I'm learning Java (The Gaming Side). I bought a book and it has some code in which I tried to copy and test it. The only problem is that it comes up with errors when I try to compile it.

C:\Users\James\Desktop\Java>Javac GamePanel.java                                        
GamePanel.java:57: illegal start of expression                              
        private void gameUpdate()                                   
        ^                         
GamePanel.java:57: illegal start of expression                             
        private void gameUpdate()                               
                ^                                              
GamePanel.java:57: ';' expected                        
        private void gameUpdate()                                 
                               ^                      
GamePanel.java:64: reached end of file while parsing                                
}→                   
 ^                                    
4 errors              

The code is:

public class GamePanel extends  JPanel implements Runnable
{
    private static final int PWIDTH = 500;
    private static final int PHEIGHT = 400;

    private Thread animator;
    private volatile boolean running = false;

    private volatile boolean gameOver = false;

    public GamePanel()
    {
        setBackground(Color.white);
        setPreferredSize( newDimension(PWIDTH, PHEIGHT));
    }

    public void addNotify()
    {
        super.addNotify();
        startGame();
    }

    public void startGame()
    {
        if (animator == null || !running)
        {
            animator = new Thread(this);
            animator.start();
        }
    }

    public void stopGame()
    {
        running = false;    
    }

    public void run()
    {
        running = true;
        while(running)
        {
            gameUpdate();
            gameRender();
            repaint();

            try
            {
                Thread.sleep(20);
            }
        catch(InterruptedException ex)
        {

        }
        System.exit(0);
    }

    private void gameUpdate()
    {
    if (gameOver == false) {

    }
    }

}

I know I'm probably doing something wrong but I checked it over and over again, can someone please enlighten me on what I am doing wrong?

Upvotes: 4

Views: 29802

Answers (9)

luc
luc

Reputation: 1

you are missing semi colons, try: private void gameUpdate();

Upvotes: 0

PETER EDWARD
PETER EDWARD

Reputation: 1

you are missing the closing brace for the While Loop Within the Run Method.....Develop using IDE like Netbeans, Eclipse etc...to avoid missing Braces because they are auto Inserted when necessary. hope it solves ur problem.

Upvotes: 0

marym
marym

Reputation: 11

Two missing end brackets! Try using an editor that will highlight such omissions. Eclipse is big & bulky but will really help you once you get past that learning curve. Notepad++ is lean & mean but you have to check for matching brackets yourself (it will find them for you).

Upvotes: 1

Gareth Davis
Gareth Davis

Reputation: 28059

Your missing a } the while loop isn't closed.

public void run()
{
    running = true;
    while(running)
    {
        gameUpdate();
        gameRender();
        repaint();

        try
        {
            Thread.sleep(20);
        }
        catch(InterruptedException ex)
        {

        }
    } // <<< this is the missing brace
    System.exit(0);
}

You might want to get an IDE like eclipse, netbeans or intellij (all free) and use them to format your code...things like missing braces become a lot easier to find when your code is correctly formatted.

Upvotes: 6

BenTuras
BenTuras

Reputation: 11

You're missing a closing } before the System.exit(0); in run()

Upvotes: 1

Mob
Mob

Reputation: 11098

You didn't close your loop }.

I suggest you use an IDE that points this out for you.

http://netbeans.org

Upvotes: 1

Nikki9696
Nikki9696

Reputation: 6348

You are missing the bracket ending your While loop.

Upvotes: 1

Grambot
Grambot

Reputation: 4514

Check your run() method. You're missing a closing curly bracket to terminate the while loop. The gameUpdate() method declaration is then being included in the previous function (incorrectly)

Upvotes: 1

Corbin
Corbin

Reputation: 33437

You missed a closing } for the while loop. And if you're wondering (and you should be), it's saying "illegal start of expression because "private ..." is trying to start a new block of code, but the Java parser knows that it hasn't finished the block it's in yet.

Upvotes: 1

Related Questions