Compile Error: illegal start of expression

Peter Dempsey :

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?

Gareth Davis :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Java Error: illegal start of expression

"Illegal start of expression" error in Java

Error in code, "illegal start of expression"

java illegal start of expression error

Error illegal start of simple expression

Receiving Illegal start of expression error

Illegal start of expression error in Java

Weird "illegal start of expression" error in Netbeans

error: illegal start of expression in Android Studio Code

javac strange syntax - error illegal start of expression

Error: illegal start of expression followed by PriorityQueue

How to fix illegal start of expression compilation error?

Error code compiling: illegal start of expression

Illegal start of expression for Annotations

Why is this an illegal start of expression?

Illegal start of expression (method)

I keep getting an "illegal start of expression" error on my code

java error in for loop condition evaluation: not a statement, illegal start of expression

What's wrong with my java program, "illegal start of expression error"

Netbeans is giving me "illegal start of expression" error message on my methods

'Try without catch error', ')' expected, illegal start of expression..?

Running into an illegal start of expression error while changing the value of an array

error: illegal start of expression in my Android studio code

Illegal Start of Expression, trying to print

Why is it saying illegal start of expression?

Why is this an illegal start of expression?and not a statement?

error: illegal start of type

This program is showing compile time error.. illegal start of type for (Integer it : arr), why it showing error?

Netbeans "illegal start of type" error