Learning better

Java error messages explained: the 10 most common ones

Published on October 22, 20266 min read

You click Run, and instead of output you get a wall of red text. That happens to everyone, professionals included, every single day. The good news: almost all beginner errors come from a handful of messages. Here you get to know the ten most common ones, each with a small broken example and the fix.

How to read a Java error message

The compiler javac translates your code before it runs. When it finds a problem, the output looks like this:

Main.java:3: error: ';' expected
        int age = 17
                    ^
1 error

It contains three pieces of information:

  1. File and line: Main.java:3 means line 3.
  2. The message: everything after error: tells you what Java is unhappy about.
  3. The caret ^: it points to the spot where Java gave up. The actual mistake is often right there or just before it.

If there are several messages, always fix the first one and compile again. A single mistake can cause a chain of follow-up errors that disappear on their own once it is fixed.

Syntax errors: Java can't parse your sentence

1. ';' expected

A statement is missing its semicolon. Java only notices at the end of the line, which is why the caret points just after the 17.

int age = 17System.out.println(age);

Fix: int age = 17;. If the message points at a line that looks fine, check the line above it. A close relative is ')' or ',' expected: a closing round bracket is missing, often right before the semicolon.

2. reached end of file while parsing

Java reached the end of the file, but a block is still open. A closing curly brace is missing.

public class Main {    public static void main(String[] args) {        System.out.println("Hello");    }

Fix: add the missing } at the end. To avoid it next time, type the matching } as soon as you type a {, and indent consistently. The format function in the editor quickly shows you where a block doesn't line up.

Names and types

3. cannot find symbol

The most common message of all. Java doesn't know a name at this point. Which name it is appears after symbol:.

Main.java:4: error: cannot find symbol
        System.out.println(Count);
                           ^
  symbol:   variable Count
  location: class Main

Typical causes:

  • Typo or wrong case: Count is not count, and string is not String.
  • Variable not declared: you use it before declaring it with a type.
  • Wrong block: a variable declared inside a loop or an if only exists in there.
  • Missing import: Scanner needs import java.util.Scanner;, as shown in the article on input and output in Java.

4. incompatible types: String cannot be converted to int

The value doesn't match the variable's type. Here, text is supposed to go into a number.

int points = "12";

Fix: either int points = 12; without quotes, or convert on purpose: int points = Integer.parseInt("12");. The message always names both types, first the one you supplied, then the one that was required.

5. incompatible types: possible lossy conversion from double to int

A double is supposed to go into an int variable. The decimal places would be lost, so Java refuses.

double average = 2.7;int rounded = average;

Fix: use the matching type (double rounded = average;) or convert explicitly: int rounded = (int) average;. Careful, that cuts off the decimals and gives 2, not 3. For rounding there is Math.round.

6. bad operand types for binary operator '>'

The operator doesn't work with these types. The classic: comparing strings with > or ==.

Main.java:9: error: bad operand types for binary operator '>'
        if (name > "Leon") { }
                 ^
  first type:  String
  second type: String

Fix: compare strings with methods. name.equals("Leon") checks for equality, name.compareTo("Leon") > 0 checks alphabetical order. More on this in the article on strings in Java.

Methods, variables and classes

7. missing return statement

The method promises to return a value, but not every path ends with return.

static int max(int a, int b) {    if (a > b) {        return a;    } else if (b >= a) {        return b;    }}

To you it's obvious that one of the cases always applies. Java doesn't check the logic, though. Fix: turn the else if into a plain else, or add a return at the end. How return values work is explained in the article on methods and recursion.

8. variable sum might not have been initialized

The variable is read before it definitely has a value. Here, sum only gets a value inside the if.

int sum;if (x > 0) {    sum = 5;}System.out.println(sum);

Fix: give the variable a starting value when you declare it, for example int sum = 0;.

9. non-static method greet() cannot be referenced from a static context

main is static. It belongs to the class, not to an object, so it can't directly use methods or variables that aren't static. For variables, the message reads non-static variable count cannot be referenced from a static context.

public class Main {    void greet() {        System.out.println("Hi");    }
    public static void main(String[] args) {        greet();    }}

Fix: write static void greet(), or create an object: new Main().greet();. What the difference really means is covered in classes and objects.

10. class Hello is public, should be declared in a file named Hello.java

A public class must have exactly the same name as its file. The file is called Main.java, but the class is called Hello.

public class Hello {    public static void main(String[] args) {        System.out.println("Hi");    }}

Fix: rename the class to Main, or rename the file to Hello.java. This often happens when you copy code from a video or a chat.

What about exceptions at runtime?

Some errors aren't reported by the compiler but only by the running program. Then you see an exception with a line number:

Output
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
	at Main.main(Main.java:5)

The array has three elements with the indexes 0 to 2, and the loop tried to read index 3. Almost always the condition says <= instead of <. See arrays in Java. The second common one is the NullPointerException, for example Cannot invoke "String.length()" because "name" is null: a variable doesn't point to any object. How to handle exceptions deliberately is explained in the article on exceptions and enums.

Understand error messages faster with Jario

If you set up Java on your own computer, there is often one error before all the others. It has its own post: “'javac' is not recognized”. In Jario that problem doesn't exist, because you write the code in the browser and it runs on our server.

With compiler errors, Jario helps you in two places. Next to the message, a “What does this mean?” hint explains the common compiler errors in plain language, which are exactly the messages from this post. If that's not enough, you can ask the AI tutor with “Explain the error”. It knows the task and your code and explains the error at your exact spot, without writing the solution for you. The editor also marks errors right in your code, and an infinite loop doesn't freeze anything. It is stopped with “Stopped: time limit exceeded. Probably an infinite loop.”

If you'd rather paste errors into a chat, learning Java with ChatGPT gives you rules so you still learn something along the way. And if your exam is written without a compiler, read how to practise for a Java exam on paper. From now on, read every message once on purpose: first message, line, caret. After a few days you'll recognise the ten classics at a glance.