Error Message: Cannot Find Symbol

What Does the 'Cannot Find Symbol' Java Error Mean?

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 - it doesn't have enough information to piece together what the Java code wants 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, like mentioned above, is related to the identifiers.

The compiler needs to know what every identifier means. If it doesn't, the code is basically looking for something that the compiler doesn't yet comprehend.

Here are some possible causes for the "Cannot Find Symbol" Java error:

  • 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 being referenced correctly using an import declaration
  • Identifiers look the same but are actually different. This 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
  • You forgot a new, like this: String s = String();, which should be String s = new String();

    Sometimes, the error is caused by a combination of some of the things mentioned above. Therefore, if you fix one thing, and the error persists, do a quick run through for each of these possible causes, one at a time.

    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 System.out 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.