error: cannot find symbol
error: cannot find symbol
DodaTech
3 min read
error: cannot find symbol means the compiler found a name it cannot resolve. A variable, class, or method doesn’t match any declaration in the current scope.
What It Means
The Java compiler works through your code and maintains a symbol table of all declared names. When it sees an identifier like userName, getTotal(), or ArrayList, it looks up that name in the symbol table. If it can’t find it, compilation stops with cannot find symbol.
Why It Happens
- You misspelled a variable, method, or class name.
- You forgot to import a class.
- You’re trying to access a method or field that doesn’t exist on the type.
- The variable is out of scope (declared in a different block).
- You’re using a class from a dependency that isn’t on the compile classpath.
- You used the wrong case for the identifier (Java is case-sensitive).
How to Fix It
1. Check for typos in the identifier
// BUG: misspelled variable name
String userName = "Alice";
System.out.println(usernName); // cannot find symbol — usernName != userName
// FIX: use the exact declared name
System.out.println(userName);2. Add the missing import
// BUG: missing import
public class MyClass {
public static void main(String[] args) {
List<String> items = new ArrayList<>(); // cannot find symbol: List
}
}
// FIX: add import at the top of the file
import java.util.List;
import java.util.ArrayList;3. Check the method or field exists on the type
String name = "Alice";
int len = name.length(); // OK — length() exists on String
int cap = name.capacity(); // cannot find symbol — String has no capacity()
// FIX: use the correct method for the type
int cap = name.length(); // or use StringBuilder if you need capacity4. Verify variable scope
// BUG: variable declared inside a block, used outside
if (true) {
int x = 10;
}
System.out.println(x); // cannot find symbol — x is out of scope
// FIX: declare in the outer scope
int x;
if (true) {
x = 10;
}
System.out.println(x);5. Make sure the class is on the compile classpath
# Compile with the required JAR
javac -cp lib/commons-lang3-3.12.0.jar MyProgram.javaIf you miss the -cp argument, symbols from the library will not be found.
6. Use the correct case
// BUG: wrong case
String s = "hello";
System.out.println(s.length); // cannot find symbol — length is a METHOD
// FIX: Java is case-sensitive
System.out.println(s.length()); // method call — correct Previous
HTTP 410 Gone — What It Means & How to Debug
Next
ERROR: Could not find a version that satisfies the requirement
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro