Reading Compiler Errors
You’re going to spend a significant portion of your life as a programmer reading error messages. That’s not pessimism, it’s just the job. The faster you can decode what went wrong, the faster you get back to writing code that actually works.
Java’s compiler is stricter than most. It refuses to produce a program until it’s satisfied everything is correct. That sounds annoying, and sometimes it is, but it also means Java catches entire categories of mistakes before your code ever runs. The trick is learning to read what the compiler is telling you, because the messages aren’t always obvious at first.
There are two kinds of errors: ones the compiler catches before running your program (compile-time errors), and ones that only appear while your program is running (runtime errors). We’ll focus mostly on compile-time errors here, since those are what you’ll hit first.
The anatomy of a compiler error
When the Java compiler finds a problem, it tells you three things:
- What file the error is in
- What line the problem is on
- What it thinks went wrong
Here’s a simple broken program:
The compiler output looks something like this:
Greeting.java:3: error: ';' expected System.out.println("Hello, world!") ^1 errorBreak that down:
Greeting.java— the file with the problem:3— the problem is on line 3error: ';' expected— what the compiler thinks is wrong- The little
^points to where in the line it got confused
In this case the fix is obvious: add the semicolon. But notice something worth remembering: the compiler points to where it got confused, not necessarily where you made the mistake. Those are often the same place, but not always.
The three most common beginner errors
Missing semicolon
Java requires a semicolon at the end of every statement. Forget one and the compiler complains, usually on the line with the missing semicolon or the line right after it.
Error message:
Example.java:3: error: ';' expected System.out.println("Line one") // missing semicolon here ^1 errorThe fix: add the semicolon at the end of line 3.
Mismatched braces
Every opening brace { needs a matching closing brace }. Forget one and the compiler often reports an error at the end of the file, which feels confusing if the actual missing brace is somewhere in the middle.
Error message:
Example.java:3: error: reached end of file while parsing System.out.println("Hello"); ^1 error"Reached end of file while parsing" is almost always a missing }. Count your braces if you see this one.
A good habit: when you type an opening brace, type the closing brace immediately before writing anything inside it. Most modern editors do this for you automatically.
Wrong class name
Java requires the class name to match the filename exactly, including capitalisation. If your file is named HelloWorld.java, the class inside must be named HelloWorld, not helloworld, not Helloworld, not hello_world.
Error message:
HelloWorld.java:1: error: class helloworld is public, should be declared in a file named helloworld.javapublic class helloworld { // wrong: lowercase ^1 errorThe fix: rename the class to match the file, or rename the file to match the class.
When the error points to the wrong place
The compiler reads your code from top to bottom. If something early throws it off, it only notices something is wrong when it hits a point where the code stops making sense, which may be lines later, or not until it runs out of file entirely.
The mismatched braces example above is a good illustration. The missing } might be somewhere in the middle of the file, but the compiler doesn’t notice until it reaches the end, reporting “reached end of file while parsing.” The real problem is several lines earlier.
The rule of thumb: if the flagged line looks fine, check the lines above it.
Runtime errors: a quick preview
Compile-time errors happen before your program runs. Runtime errors are different. They slip past the compiler just fine, but crash the program while it’s actually running.
Click Run on this program. It will compile successfully, then immediately crash:
You’ll see something like:
Exception in thread "main" java.lang.ArithmeticException: / by zero at Example.main(Example.java:3)Don’t worry about understanding what ArithmeticException means yet. The important parts for now are the short description (/ by zero) and the line number (Example.java:3). That’s where you start looking.
You’ll encounter runtime errors regularly as you progress. There’s a full lesson on reading them properly later in the course. For now, just know that when your program crashes, Java tells you what went wrong and where.
Checkpoint
Your program compiles, but the moment it runs it stops with an error message. What does this tell you?
You compile this program and the error message says “reached end of file while parsing.” Nothing looks wrong to you. What should you check first?
public class Example {
public static void main(String[] args) {
System.out.println("Hello");
System.out.println("Goodbye");
You name your file MyProgram.java but declare public class myprogram inside it. What happens?