Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compile Error: illegal start of expression

Tags:

java

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?

like image 509
Peter Dempsey Avatar asked Dec 16 '22 08:12

Peter Dempsey


2 Answers

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.

like image 62
Gareth Davis Avatar answered Dec 24 '22 13:12

Gareth Davis


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.

like image 30
Corbin Avatar answered Dec 24 '22 14:12

Corbin