Free: 4 trials of 20 minutes, no cardStart free

Craftstack Academy
Java

Java error: cannot find symbol, and how to fix it

· 10 min read

You compile a short program and javac stops with a message that seems to say very little:

Terminal
Main.java:4: error: cannot find symbol
        System.out.println(hour * 365);
                           ^
  symbol:   variable hour
  location: class Main
1 error

error: cannot find symbol means the compiler met a name, a symbol, that it doesn't know: a variable, a method or a class that, as far as Java can see, doesn't exist at that point in your code. It's the most common Java compile error for beginners, and the message tells you more than it seems: the symbol: line names exactly what's missing, and the location: line says where Java looked. Here, the variable is called hours, but the code uses hour. This article shows how to read those two lines and fix the eight usual causes.

Try it yourself

Save this as Main.java and run javac Main.java:

Main.java
public class Main {
    public static void main(String[] args) {
        int hours = 3;
        System.out.println(hour * 365);
    }
}

You'll get the error above. Change hour to hours, compile again, and run java Main: it prints 1095.

How to read "cannot find symbol"

The message has four parts, and the last two are the important ones:

  • Main.java:4: the file and line.
  • The ^ mark: the exact spot. It points at the start of the unknown name.
  • symbol: variable hour: what Java couldn't find, and what kind of thing it thought it was: a variable, a method or a class.
  • location: class Main: where Java looked for it. For a method called on an object, this line names the object and its type.

So ask two questions: is the name spelled exactly like the place where I created it? And: can Java see that place from here?

Cause 1: a typo or a capital letter

The example above. Java names are case-sensitive: hours, hour, Hours and HOURS are four different names. The same goes for built-in names:

java
string name = "Alex";
system.out.println(name);
Terminal
Main.java:3: error: cannot find symbol
        string name = "Alex";
        ^
  symbol:   class string
  location: class Main
Main.java:4: error: package system does not exist
        system.out.println(name);
              ^
2 errors

String and System are classes, and Java class names start with a capital letter. Notice that the lowercase system gives a different message: Java guesses that system.out might be a package name, and says the package doesn't exist. Same mistake, same fix.

Cause 2: a misspelled method

When the unknown name is a method, the symbol: line shows method and the types of the arguments:

java
String name = "Alex";
System.out.println(name.lenght());
Terminal
Main.java:4: error: cannot find symbol
        System.out.println(name.lenght());
                               ^
  symbol:   method lenght()
  location: variable name of type String
1 error

The location: line is the useful one here: Java looked for lenght() in name, which is a String, and String has no such method. The correct spelling is length().

The same with a capital letter in a built-in method:

Terminal
Main.java:3: error: cannot find symbol
        Integer.parseint("5");
               ^
  symbol:   method parseint(String)
  location: class Integer
1 error

It's Integer.parseInt, with a capital I. In Java, method names use camelCase: each new word starts with a capital letter.

Cause 3: a missing import

Classes outside the java.lang package must be imported. The classic one is Scanner:

java
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
    }
}
Terminal
Main.java:3: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
        ^
  symbol:   class Scanner
  location: class Main
Main.java:3: error: cannot find symbol
        Scanner scanner = new Scanner(System.in);
                              ^
  symbol:   class Scanner
  location: class Main
2 errors

Two errors for one mistake: Java can't find Scanner as a type, and again after new. Add the import above the class:

java
import java.util.Scanner;

Other classes beginners often forget to import: ArrayList, HashMap, List, Random (all in java.util), and LocalDate (in java.time). String, Math, Integer and System never need an import, because they live in java.lang.

Cause 4: a variable used outside its block

A variable exists only inside the { } block where it was declared. That's called its scope. Outside the block, Java can't see it:

java
int hours = 3;
if (hours > 2) {
    String label = "a lot";
}
System.out.println(label);
Terminal
Main.java:7: error: cannot find symbol
        System.out.println(label);
                           ^
  symbol:   variable label
  location: class Main
1 error

label was created inside the if block and disappeared at its closing }. Declare it before the block and give it a value inside:

java
String label = "a little";
if (hours > 2) {
    label = "a lot";
}
System.out.println(label);

The same rule applies to loops. A variable declared inside a for loop, or the loop counter i itself, doesn't exist after the loop ends.

Cause 5: a method that doesn't exist (yet)

java
int total = add(2, 3);
Terminal
Main.java:3: error: cannot find symbol
        int total = add(2, 3);
                    ^
  symbol:   method add(int,int)
  location: class Main
1 error

symbol: method add(int,int) shows what Java searched for: a method called add that takes two int values. Either the method isn't written yet, it's in a different class, or its name is different. Write it, or call it through the right class.

If the method exists but you call it with the wrong number or types of arguments, you get a different error that lists what it expected:

Terminal
Main.java:4: error: method add in class Main cannot be applied to given types;
        System.out.println(add(2));
                           ^
  required: int,int
  found:    int
  reason: actual and formal argument lists differ in length
1 error

Cause 6: length, length() and size()

Java uses three different ways to ask "how many", and mixing them up gives cannot find symbol:

java
ArrayList<String> names = new ArrayList<>();
System.out.println(names.length);
int[] nums = {1, 2};
System.out.println(nums.size());
Terminal
Main.java:6: error: cannot find symbol
        System.out.println(names.length);
                                ^
  symbol:   variable length
  location: variable names of type ArrayList<String>
Main.java:8: error: cannot find symbol
        System.out.println(nums.size());
                               ^
  symbol:   method size()
  location: variable nums of type int[]
2 errors

The rule to remember:

  • Arrays use length, without brackets: nums.length.
  • Strings use length(), with brackets: name.length().
  • Lists (ArrayList) use size(): names.size().

The location: line tells you which one you have: of type int[] is an array, of type ArrayList<String> is a list.

Cause 7: a class in another file

If your program uses a class from another file, like Dog.java, Java needs to find that file:

Terminal
Main.java:3: error: cannot find symbol
        Dog d = new Dog();
        ^
  symbol:   class Dog
  location: class Main

Check these three things:

  1. The file is named exactly like the class, Dog.java, capital letter included.
  2. It's in the same folder as Main.java (or in the right package folder).
  3. You compile with javac Main.java from that folder. In our test with JDK 21, javac found Dog.java next to Main.java by itself and the program ran. But running java Main.java directly, the single-file shortcut, gave this error, because that mode compiles one file only.

Cause 8: your editor's wording

Different tools describe the same problem in their own words. You may see:

Editor
java: cannot find symbol
Cannot resolve symbol 'hour'
hour cannot be resolved to a variable

The first is the same javac message as shown by IntelliJ IDEA's build window. The second is the red-underline wording in IntelliJ, and the third is how Eclipse says it (Eclipse also says cannot be resolved to a type for classes). All three have the same causes and the same fixes.

Errors that look alike

  • package system does not exist: a lowercase class name used like a package (cause 1).
  • method X in class Y cannot be applied to given types: the method exists, but the arguments don't match (cause 5).
  • incompatible types: String cannot be converted to int: the name exists, but the value has the wrong type. See String cannot be converted to int.
  • class, interface, enum, or record expected: usually a brace in the wrong place. See reached end of file while parsing.

Wrong fixes that don't help

  • Declaring the variable again where the error is. A new String label; before the println removes the error and prints the wrong thing (or causes might not have been initialized). Fix the scope instead.
  • Importing everything with import java.util.*; for every error. It helps only when a class from java.util is missing. A misspelled name stays misspelled.
  • Fixing only the first of several errors and giving up. One missing import often causes several cannot find symbol errors. Fix the first, compile again, and the count usually drops.

A debugging walkthrough

javac reports five cannot find symbol errors in a 40-line program:

  1. Start with the first error. Later ones are often caused by it.
  2. Read the symbol: line. Is it a class, a method or a variable?
  3. For a class: is the capital letter right, and is the import there?
  4. For a method: compare with the documentation or the definition, letter by letter. Check the location: type.
  5. For a variable: find where it's declared. Is that inside a { } block that has already closed?
  6. Compile again after each fix.

Practice

Predict the symbol: line for each.

  1. Math.sqroot(16)
  2. ArrayList<Integer> list = new ArrayList<>(); without any import.
  3. for (int i = 0; i < 3; i++) { } System.out.println(i);

Answers. (1) symbol: method sqroot(int), location class Math: the method is Math.sqrt. (2) symbol: class ArrayList: add import java.util.ArrayList;. (3) symbol: variable i: the loop counter exists only inside the loop.

Frequently asked questions

What does "cannot find symbol" mean in Java?

The compiler found a name it doesn't know at that point: a variable, method or class that is misspelled, not imported, not written yet, or out of scope.

What is a symbol in Java?

Any name in your code: a variable, a method, a class or a package. The symbol: line says which kind Java was looking for.

How do I fix cannot find symbol for Scanner?

Add import java.util.Scanner; at the top of the file, above the class.

Why does Java say cannot find symbol when the variable exists?

It exists somewhere else: inside a block that has already closed, in another method, or with a different spelling or capital letter.

Is "cannot find symbol" a compile error or a runtime error?

A compile error. javac stops, and no program runs until you fix it.

A quick checklist

  1. Read the symbol: line: is it a variable, method or class?
  2. Compare the spelling, including capitals, with where it's declared.
  3. For classes, check the import and the file name.
  4. For variables, check that you're inside the block that declared them.
  5. For "how many": arrays length, strings length(), lists size().
  6. Fix the first error, then compile again.

For a method that works on any error, read how to read an error message. The official Java tutorial on variables explains naming rules.

1 beginner error a week

One common error, what it means and how to fix it, in a short email. Free, and you can unsubscribe in any email.

Java

bad operand types for binary operator (Java fix)

Fix Java's bad operand types for binary operator '&&', '<', '>=' or '*': what first type and second type mean, the causes and fixes, with examples.

Error explained · · 13 min read

Learn by writing the code yourself.

Twenty minutes with the tutor, right in your browser. No card, nothing to install.

Start the free trial
A session: the tutor's chat on the left, the code editor with a Java program and its output on the right