Do My Programming HomeworkDo My Programming Homework

Java “cannot find symbol”: every cause and how to fix it

The compiler met a name it has no declaration for. Here is how to read the message, the 8 causes with real javac output, and the errors that look similar but aren’t.

DoMyProgrammingHomework.io engineering teamReviewed by Pollen Victory, founderPublished Nov 22, 2026 · 8 min read
A Java line that uses a misspelled variable, and the compiler message error: cannot find symbol
The short answer
“cannot find symbol” is a compile error. It means the Java compiler met a name it has no declaration for at that point: a variable, a method or a class. The two lines under the error tell you which name and where: read symbol: and location:. The usual causes are a typo or a wrong capital letter, a variable used outside the block it was declared in, a missing import, or a method the type doesn’t have.

The message is better than it looks. It already tells you what the compiler was looking for. You only need to know how to read it.

We compiled every example below with javac on Java 21 before writing it down. The messages are copied from those runs.

How to read the message

What javac printstext
Main.java:4: error: cannot find symbol
        totl = total + 5;
        ^
  symbol:   variable totl
  location: class Main

There are four parts.

  • Main.java:4 is the file and the line number.
  • The caret (^) points at the exact name the compiler could not find.
  • symbol: says what kind of name it is, and repeats it. This is the line that matters most.
  • location: says where the compiler was looking.
What the symbol line is telling you
The symbol line saysThe compiler was looking forCheck first
symbol: variable totlA variable or field with that exact nameSpelling, capital letters, and where it was declared
symbol: method addall(int,int)A method with that name and those argument typesThe method’s spelling, and what the type really offers
symbol: class ScannerA class or other typeA missing import, the spelling, or a file the compiler can’t see

The 8 causes

1. A typo or a wrong capital letter

Java treats total, Total and totl as three different names. Oracle’s Java tutorial puts it in four words: “Variable names are case-sensitive.”

Fails: two names that were never declaredjava
int total = 0;
totl = total + 5;
System.out.println(Total);
Passes: one name, spelled one wayjava
int total = 0;
total = total + 5;
System.out.println(total);

The same applies to types. Writing string label gave us symbol: class string, because the type is String.

2. A variable used outside its block

A variable exists only inside the braces where it was declared. The Java language specification says the scope of a local variable declared in a block “is the rest of the block”. After the closing brace, the name is gone.

Fails: sum and i only exist inside the loopjava
for (int i = 0; i < 3; i++) {
    int sum = i * 2;
}
System.out.println(sum);
System.out.println(i);
Passes: declare sum before the loopjava
int sum = 0;
for (int i = 0; i < 3; i++) {
    sum += i * 2;
}
System.out.println(sum);

This catches people with if blocks and try blocks too. If you need a value after the block, declare the variable before it.

3. A variable used before it is declared

The declaration has to come first.

Fails: count is used on the line above its declarationjava
count = count + 1;
int count = 0;
Passesjava
int count = 0;
count = count + 1;

4. A missing import

Scanner and ArrayList are not available automatically. They live in the java.util package, and you have to say so.

Fails with "symbol: class Scanner" and "symbol: class ArrayList"java
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<String> names = new ArrayList<>();
    }
}
Passes: the imports go at the top of the filejava
import java.util.ArrayList;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        ArrayList<String> names = new ArrayList<>();
    }
}

Oracle’s tutorial lists three ways to use a class from another package: “Refer to the member by its fully qualified name”, “Import the package member”, or “Import the member’s entire package”. For homework, the second is the usual choice.

Those two lines produced four errors for us, two for each class. That is normal. Fix the import and all four go away.

5. The wrong way to ask for a length

Strings, arrays and lists each do it differently, and mixing them up is one of the most common versions of this error.

How to get the size of each type, and what javac said when we got it wrong
TypeCorrectWrongThe symbol line
Stringname.length()name.lengthsymbol: variable length
Arrayscores.lengthscores.length()symbol: method length()
ArrayListlist.size()list.length()symbol: method length()

A string’s length is a method, so it needs brackets. An array’s length is what Oracle’s tutorial calls “the built-in length property”, so it has none.

6. A misspelled method name

Fails with "symbol: method addall(int,int)"java
static int addAll(int a, int b) {
    return a + b;
}

public static void main(String[] args) {
    System.out.println(addall(2, 3));
}
Passes: the call matches the declaration, capital letter includedjava
System.out.println(addAll(2, 3));

Read the symbol line closely here. It shows the argument types the compiler was looking for, (int,int). A method’s name and its parameter types together are what Oracle’s tutorial calls “the method signature”.

7. A missing “new”

Without new, Java reads the class name as a method call.

Fails with "symbol: method String(String)"java
String s = String("hi");
Passesjava
String s = new String("hi");

8. A class the compiler can’t see

If the symbol line says class Student and Student is your own class, the compiler did not find its file.

  • The file has another name. Oracle’s tutorial says to put a class in a file “whose name is the simple name of the type and whose extension is .java”. We put Student in a file called Pupil.java. Compiling Main.java on its own failed with symbol: class Student.
  • The file is in another folder. The javac documentation says that when no class path is given, it uses the CLASSPATH setting if there is one, “or else the current directory”. A class somewhere else is invisible unless you tell the compiler where it is.
  • Not every file was compiled. Compiling everything together, with javac *.java, fixed our misnamed file case.
  • The package doesn’t match. An import for a package that isn’t there gave us a second message as well: package school does not exist.

The same thing happens with a method. Calling s.getGrade() when Student only has getName() gave us symbol: method getGrade() with location: variable s of type Student. The location line tells you which class to open.

When the grader says it and your computer doesn’t
An autograder compiles your file together with its own tests. If a test calls list.addFirst(x) and you named the method addFront, the grader reports “cannot find symbol”, and the file named in the message is the test, not yours. Copy method names from the assignment exactly. More in our guide to why code works locally but fails the autograder.

Errors that look similar and aren’t

We produced each of these while testing. Each one means something different, and the fix is different.

Five other messages, and what each one means
MessageWhat it means
incompatible types: String cannot be converted to intThe method exists. You passed the wrong type.
method square in class Main cannot be applied to given typesThe method exists. You passed the wrong number of arguments.
name has private access in StudentThe field exists. It is private, so only its own class can use it.
package system does not existYou wrote system.out with a small s. It is System.out.
variable count might not have been initializedThe variable is declared. It was never given a value.

A fix order that works

From the first error to a clean compile
  1. Start with the first errorOne mistake can cause several messages. Fix the top one and compile again before reading the rest.
  2. Read the symbol lineIt tells you whether the compiler wants a variable, a method or a class.
  3. Check the spelling, letter by letterCompare the name with its declaration. Capital letters count.
  4. Find the declarationIs it above this line? Is it inside the same braces?
  5. For a class, check the import and the fileIs there an import? Does the file name match the class name? Is the file in this folder?
  6. For a method, open the typeLook at what the class really offers. Check the name and the argument types.

Still stuck?

Send us your files and the compiler output. A senior engineer finds the cause, fixes it and explains why it broke, so you can explain it too. On our debug my code page, a single bug or error message is typically $45–$70, usually the same day.

Free price checkFind out what a fix would costUpload your code and the error message. You see a price range in under a minute, with no email needed. Your fixed price never goes above the top of that range.
Check your course rules first
Rules on outside help differ between schools, courses and assignments. Read your syllabus and honor code before you use any help, ours included. Our academic integrity page says what we do and what we won’t.

Related: Java homework help, data structures homework help and code review before you submit. New to the language? Start with what Java does.

Questions about this error

What does cannot find symbol mean in Java?

It means the compiler met a name it has no declaration for at that point in your code. The name can be a variable, a method or a class. The symbol line under the error says which name, and the location line says where the compiler was looking.

Why do I get cannot find symbol when the variable is declared?

Usually because it is declared somewhere the failing line can’t see. A variable declared inside a loop or an if block stops existing at the closing brace. A different capital letter, or a declaration further down the method, causes the same message.

How do I fix cannot find symbol class Scanner?

Add an import for java.util.Scanner at the top of the file, above the class declaration. The Scanner class lives in the java.util package and is not available until you import it.

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

It is a compile error. The compiler reports it before the program runs, so you see it when you build, not when you run.

Why does javac say cannot find symbol for a class in the same folder?

Check that the file name matches the class name exactly, with the same capital letters, and that the file is really in the folder you are compiling from. Compiling all the files together is a quick way to rule out a file that was left out.

Why do I get several cannot find symbol errors for one mistake?

Because the compiler reports every place it could not resolve the name. A missing import produces an error each time the class is used. Fix the first error, compile again, and most of the others usually disappear.

Sources

Stuck on your own assignment?

Upload the prompt and see a predicted budget in under a minute. A senior engineer confirms the final price.

Keep reading

Related guides