Error Message: Cannot Find Symbol

Don't misplace your identifiers in your Java code!

Hands using a keyboard
Savas Keskiner/E+/Getty Images

When a Java program is being compiled, the compiler creates a list of all the identifiers in use. If it can't find what an identifier refers to (e.g., there is no declaration statement for a variable) it cannot complete the compilation.

This is what the

cannot find symbol
error message is saying—the compiler doesn't have enough information to piece together what the Java code is intended to execute.

Possible Causes For the "Cannot Find Symbol" Error

Although the Java source code contains other things like keywords, comments, and operators, the "Cannot Find Symbol" error references the name of a specific package, interface, class, method or variable. The compiler needs to know what every identifier references. If it doesn't, the code is basically looking for something that the compiler doesn't yet comprehend.

Some possible causes for the "Cannot Find Symbol" Java error include:

  • Trying to use a variable without declaring it.
  • Misspelling a class or method name. Remember that Java is case sensitive and spelling errors are not corrected for you. Also, underscores may or may not be necessary, so watch out for code that use them when they shouldn't be used or vice versa.
  • The parameters used do not match a method's signature.
  • The packaged class has not been referenced correctly using an import declaration.
  • Identifiers look the same but are actually different. This problem can be hard to spot, but in this case, if the source files use UTF-8 encoding, you may be using some identifiers as if they're identical but really they're not because they simply appear to be spelled the same.
  • You're looking at the wrong source code. It may seem hard to believe that you're reading a different source code than the one producing the error, but it's definitely possible, and especially for new Java programmers. Check file names and version histories carefully.
  • You forgot a new, like this: 
    String s = String();
    , which should be 
    String s = new String();

    Sometimes, the error arises from a combination of problems. Therefore, if you fix one thing, and the error persists, check for different problems still affecting your code.

    For example, it's possible that you are trying to use an undeclared variable and when you fix it, the code still contains spelling errors.

    Example of a "Cannot Find Symbol" Java Error

    Let's use this code as an example:

    System.out.prontln("The perils of mistyping..");

    This code will cause a

    cannot find symbol
    error because the
    class does not have a method called “prontln”:
    cannot find symbol
     symbol: method prontln(jav.lang.String)
     location: class

    The two lines below the message will explain exactly what part of the code is confusing the compiler.

    Mistakes like capitalization mismatches are often flagged in a dedicated integrated development environment. Although you can write your Java code in any text editor, using IDEs and their associated linting tools reduces typos and mismatches. Common Java IDEs include Eclipse and NetBeans.