Programming Errors
Debugging
Coding
Java
Symbol Resolution

What does a Cannot find symbol or Cannot resolve symbol error mean?

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

When programming in Java or using similar languages supporting static type checking, you might encounter the error messages "Cannot find symbol" or "Cannot resolve symbol". These errors are compile-time issues that indicate the compiler cannot recognize a reference to a symbol, where a symbol can be a variable, method, class, or package name.

Understanding the Error

The "Cannot find symbol" error is commonly seen when using the Java Development Kit (JDK) to compile Java code. This error points out that the Java compiler cannot recognize a symbol that is being referred to in your code. This usually suggests a few potential problems:

  • Spelling Mistakes: The most common cause is simple typos or spelling mistakes in your code.
  • Incorrect import statements: If the symbol is a class or a package that you are trying to use, it could be that you have not imported it correctly, or at all.
  • Scope of the symbol: Perhaps the variable is declared in a different scope from which it is being accessed.
  • Library not linked: If the symbol belongs to a library, the library may not be properly linked or added to the project’s build path.

Common Scenarios and Examples

1. Variable Mismatch

If you declare a variable with one name but use a different name later on, you will encounter this error.

java
1public class Test {
2    public static void main(String[] args) {
3        int number = 10;
4        numbr = 15;  // Error: Cannot find symbol
5    }
6}

2. Method Name Typo

Similarly, calling a method with an incorrect name will result in this error.

java
1public class Test {
2    public void displayMessage() {
3        System.out.println("Hello!");
4    }
5
6    public static void main(String[] args) {
7        Test test = new Test();
8        test.dispalyMessage();  // Error: Cannot find symbol
9    }
10}

3. Incorrect Import

Using a class without the appropriate import statement, or importing it incorrectly, can also lead to this error.

java
1import java.utl.List;  // Error: package java.utl does not exist
2
3public class Test {
4    List<String> names;
5}

4. Class or Package Not Found

If you refer to a class that does not exist or is not in the classpath, this will trigger the error.

java
public class Test {
    NonExistentClass obj;  // Error: Cannot find symbol
}

Resolving the Error

To solve a "Cannot find symbol" error, you will need to:

  • Verify spelling and case sensitivity: Ensure all identifiers are spelled correctly and cases are respected.
  • Correct imports: Confirm that all import statements are correctly spelled and refer to valid classes or packages.
  • Ensure library dependencies are properly added to your project’s build path: If the symbol is from a library, ensure the library is correctly linked.

Debugging Tips

  • Check the line number: The error message provides a line number that directs you where to look.
  • Use an IDE: Integrated Development Environments (IDEs) like IntelliJ IDEA or Eclipse offer features like code suggestions that can prevent this error.
  • Read the environment setup documentation: Make sure the project setup, including libraries and dependencies, matches the requirements.

Summary Table

Issue TypeCommon CausesExampleSolution
Spelling MistakesTypo in variable, method, or classint numbr = 10;Check spelling, particularly of identifiers.
Incorrect Import StatementsWrong or missing importimport java.utl.ArrayList;Fix imports to properly reference classes/packages.
Scope IssuesVariable not in scopeUsing a local variable outside its scopeVerify variable declaration and usage scope.
Library or Classpath IssuesMissing libraries or classpath issueNonExistentClass obj;Add necessary libraries and ensure they are linked.

By knowing how to identify and resolve the "Cannot find symbol" error, you can streamline your development process and reduce potential frustrations during Java project compilation.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms