Common Beginner Compile Errors

Naveen AnamaneniUpdated · 6 min read

Red error text after javac can feel like the computer is telling you off. It isn't. A compile error is the compiler doing you a favor: it found a problem before your program reached a user. Every message follows the same pattern, and the same eight or so errors cause most beginners' trouble. Learn to read them once and they stop being scary.

How to Read an Error Message

The five parts of every compile error
PartMeaning
Errs.javaThe file with the problem
:3:The line number
error: ';' expectedWhat the compiler expected or didn't understand
The copied line and ^Where on the line it noticed the problem
1 errorHow many errors in total

The Errors You'll Meet Most

Each message below is real javac output from Java 25.

1. ';' Expected

Compiler output
Errs1.java:3: error: ';' expected
        System.out.println("Hi")
                                ^

Cause: the statement doesn't end with a semicolon. Fix: add ; where the ^ points. Sometimes the ^ points just after the line that's really missing it.

2. Cannot Find Symbol

Compiler output
Errs2.java:4: error: cannot find symbol
        System.out.println(totl);
                           ^
  symbol:   variable totl
  location: class Errs2

Cause: Java doesn't know the name totl. Usually it's a spelling mistake (totl instead of total), a variable used outside the braces it was declared in, or a missing import. Fix: check the spelling and capital letters of the name shown after symbol:.

3. Package system Does Not Exist

Compiler output
Errs3.java:3: error: package system does not exist
        system.out.println("Hi");
              ^

Cause: system with a small s. Java is case-sensitive, so it thinks system is a package name. Fix: write System.

4. Class Is Public, Should Be Declared in a File Named ...

Compiler output
Wrong.java:1: error: class Right is public, should be declared in a file named Right.java
public class Right {
       ^

Cause: the file name and the public class name don't match. Fix: rename the file to Right.java, or rename the class to Wrong.

5. Incompatible Types

Compiler output
Errs5.java:3: error: incompatible types: String cannot be converted to int
        int count = "five";
                    ^

Cause: a value of one type is put where another type is needed. Here, text goes into an int. Fix: use the right type (int count = 5;), or convert it, for example with Integer.parseInt("5").

6. Variable Might Not Have Been Initialized

Compiler output
Errs6.java:4: error: variable x might not have been initialized
        System.out.println(x);
                           ^

Cause: a local variable is used before it has a value. Java never guesses a default for local variables. Fix: give it a value when you declare it, such as int x = 0;.

7. Missing Return Statement

Compiler output
Errs7.java:4: error: missing return statement
    }
    ^

Cause: a method promises to return a value (for example static int twice(int n)), but there's a path where it ends without return. Fix: add return result; before the closing brace.

8. Reached End of File While Parsing

Compiler output
Errs8.java:4: error: reached end of file while parsing
    }
     ^

Cause: a { was never closed, so the file ended while Java was still inside a class or method. Fix: count your braces. A good editor highlights the matching brace when the cursor is next to one.

Fix a Broken Program

This is the corrected version of a program that had three of the errors above: a missing semicolon, system with a small s, and a misspelled variable name.

Receipt.java▶ Try it
public class Receipt {
    public static void main(String[] args) {
        int items = 4;
        int pricePerItem = 60;
        int total = items * pricePerItem;
        System.out.println("Items: " + items);
        System.out.println("Total: Rs. " + total);
    }
}
Output
Items: 4
Total: Rs. 240

How it works

  1. Line 3 originally read int items = 4 with no semicolon, which gave ';' expected.
  2. Line 6 originally used system.out, which gave package system does not exist.
  3. Line 5 originally used pricePerltem (with a lowercase L instead of an I), which gave cannot find symbol.
  4. With all three fixed, javac prints nothing and the program runs.

Try it yourself: put back one of the mistakes at a time, compile, and match the message to the list above.

Not a Compile Error: Launcher Messages

Some errors appear only when you run java, after compiling succeeded. They come from the launcher, not from javac:

MessageUsual cause
Error: Could not find or load main class BillWrong class name, .class added to the command, or the wrong folder
Error: Main method not found in class FareThe class has no main method, or main is spelled Main or is private

Common Mistakes

Reading only the last line. 1 error tells you nothing. The useful part is the first line: file, line number and message.

Changing code at random until it compiles. Read the message, find the line, fix the one thing it describes. Random changes create new errors.

Ignoring warnings. Lines that say warning: don't stop compilation, but they often point to a real bug. Read them too.

Practice

  1. Easy: For each message, name the likely fix: ';' expected, cannot find symbol, reached end of file while parsing.
  2. Harder: Write a method static int discount(int price) that returns 10% of the price, but leave out the return line on purpose. Compile it, read the error, then fix it.

Quick check: Compile errors

4 questions
1In Bill.java:7: error: cannot find symbol, what does 7 tell you?
2Which mistake gives package system does not exist?
3You get 12 errors after forgetting one closing brace. What should you do?
4variable x might not have been initialized means:

Key Takeaways

Was this tutorial helpful?

Lesson 21 of 482