HiveBrain v1.2.0
Get Started
← Back to all entries
debugjavaCritical

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

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
errorfindresolvedoesmeancannotwhatsymbol

Problem

Please explain the following about "Cannot find symbol", "Cannot resolve symbol" or "Symbol not found" errors (in Java):

  • What do they mean?



  • What things can cause them?



  • How does the programmer go about fixing them?



This question is designed to seed a comprehensive Q&A about these common compilation errors in Java.

Solution


  1. Is there any difference between these errors?



Not really. "Cannot find symbol", "Cannot resolve symbol" and "Symbol not found" all mean the same thing. (Different Java compilers are written by different people, and different people use different phraseology to say the same thing.)
  1. What does a "Cannot find symbol" error mean?



Firstly, it is a compilation error1. It means that either there is a problem in your Java source code, or there is a problem in the way that you are compiling it.

Your Java source code consists of the following things:

  • Keywords: like class, while, and so on.



  • Literals: like true, false, 42, 'X' and "Hi mum!".



  • Operators and other non-alphanumeric tokens: like +, =, {, and so on.



  • Identifiers: like Reader, i, toString, processEquibalancedElephants, and so on.



  • Comments and whitespace.



A "Cannot find symbol" error is about the identifiers. When your code is compiled, the compiler needs to work out what each and every identifier in your code means.

A "Cannot find symbol" error means that the compiler cannot do this. Your code appears to be referring to something that the compiler doesn't understand.
  1. What can cause a "Cannot find symbol" error?



As a first order, there is only one cause. The compiler looked in all of the places where the identifier should be defined, and it couldn't find the definition. This could be caused by a number of things. The common ones are as follows:

-
For identifiers in general:

  • Perhaps you spelled the name incorrectly; i.e., StringBiulder instead of StringBuilder. Java cannot and will not attempt to compensate for bad spelling or typing errors.



  • Perhaps you got the case wrong; i.e. stringBuilder instead of StringBuilder. All Java identifiers are case sensitive.



  • Perhaps you used underscores inappropriately; i.e., mystring and my_string are different. (If you stick to the Java style rules, you will be largely protected from this mistake...)



  • Perhaps you are trying to use something that was declared "somewhere else"; i.e. in a different context to where you have implicitly told the compiler to look. (A different class? A different scope? A different package? A different code-base?)



-
For identifiers that should refer to variables:

  • Perhaps you forgot to declare the variable.



  • Perhaps the variable declaration is out of scope at the point you tried to use it. (See example below)



-
For identifiers that should be method or field names:

-
Perhaps you are trying to refer to an inherited method or field that wasn't declared in the parent / ancestor classes or interfaces.

-
Perhaps you are trying to refer to a method or field that does not exist (i.e., has not been declared) in the type you are using; e.g., "rope".push()2.

-
Perhaps you are trying to use a method as a field, or vice versa; e.g. "rope".length or someArray.length().

-
Perhaps you are mistakenly operating on an array rather than array element; e.g.,

String strings[] = ...
    if (strings.charAt(3)) { ... }
    // Maybe that should be 'strings[0].charAt(3)'


-
For identifiers that should be class names:

-
Perhaps you forgot to import the class.

-
Perhaps you used "star" imports, but the class isn't defined in any of the packages that you imported.

-
Perhaps you forgot a new as in:

String s = String();  // Should be 'new String()'


-
Perhaps you are trying to import or otherwise use a class that has been declared in the default package; i.e., the one where classes with no package statements go.

Hint: learn about packages. You should only use the default package for simple applications that consist of one class ... or at a stretch, one Java source file.

-
For cases where type or instance doesn't appear to have the member (e.g., method or field) you were expecting it to have:

  • Perhaps you have declared a nested class or a generic parameter that shadows the type you were meaning to use.



  • Perhaps you are shadowing a static or instance variable.



  • Perhaps you imported the wrong type; e.g., due to IDE completion or auto-correction may have suggested java.awt.List rather than java.util.List.



  • Perhaps you are using (compiling against) the wrong version of an API.



  • Perhaps you forgot to cast your object to an appropriate subclass.



  • Perhaps you have declared the variable's type to be a supertype of the one with the member you are looking for.



The problem is often a combination of the above. For example, maybe you "star" imported java.io.* and then tried to use the Files class ... which is in java.nio not java.io. Or maybe you meant to write File ... which is a class in java.io.

Here is an example of how incorrect variable scoping can lead to a "Cannot find symbol" error:

```
List strings = ...

for (int i = 0; i < strings.size(); i++) {
if (strings.get(i).equalsIgnoreCase("fnord")) {
break;
}
}
if (i < strings.size()) {
...

Code Snippets

String strings[] = ...
    if (strings.charAt(3)) { ... }
    // Maybe that should be 'strings[0].charAt(3)'
String s = String();  // Should be 'new String()'
List<String> strings = ...

for (int i = 0; i < strings.size(); i++) {
    if (strings.get(i).equalsIgnoreCase("fnord")) {
        break;
    }
}
if (i < strings.size()) {
    ...
}
for (int i = 0; i < 100; i++); {
    System.out.println("i is " + i);
}
for (int i = 0; i < 100; i++);

// The previous and following are separate statements!!

{
    System.out.println("i is " + i);
}

Context

Stack Overflow Q#25706216, score: 565

Revisions (0)

No revisions yet.