text: Query:
  Please explain the following about “Cannot find symbol”, “Cannot resolve symbol” or “Symbol not found” errors (in Java).
  “Cannot find symbol”, “Cannot resolve symbol” and “Symbol not found” all mean the same thing. Different Java compilers use different phraseology.
  What does a “Cannot find symbol” error mean? Answer #1:
  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.
  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.
  StringBiulderinstead of
  StringBuilder. Java cannot and will not attempt to compensate for bad spelling or typing errors.
  - Perhaps you got the case wrong; i.e.
  stringBuilderinstead of
  StringBuilder. All Java identifiers are case sensitive.
  - Perhaps you used underscores inappropriately; i.e.
  mystringand
  my_stringare 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".lengthor
  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
  newas in:
  String s = String(); // should be 'new String()'
  - 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.Listrather 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 your 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<String> strings = ... for (int i = 0; i < strings.size(); i++) { if (strings.get(i).equalsIgnoreCase("fn.)
  Here is an example that causes puzzlement where a typo leads to a seemingly inexplicable “Cannot find symbol” error:
  for (int i = 0; i < 100; i++); { System.out.println("i is " + i); }
  This will give you a compilation error in the
  println call saying that
  i cannot be found. But (I hear you say) I did declare it!
  The problem is the sneaky semicolon (
  ; ) before the
  {. The Java language syntax defines a semicolon in that context to be an empty statement. The empty statement then becomes the body of the
  for loop. So that code actually means this:
  for (int i = 0; i < 100; i++); // The previous and following are separate statements!! { System.out.println("i is " + i); }
  The
  { ... } block is NOT the body of the
  for loop, and therefore the previous declaration of
  i in the
  for statement is out of scope in the block.
  Here is another example of “Cannot find symbol” error that is caused by a typo.
  int tmp = ... int res = tmp(a + b);
  Despite the previous declaration, the
  tmp in the
  tmp(...) expression is erroneous. The compiler will look for a method called
  tmp, and won’t find one. The previously declared
  tmp is in the namespace for variables, not the namespace for methods.
  In the example I came across, the programmer had actually left out an operator. What he meant to write was this:
  int res = tmp * (a + b);
  There is another reason why the compiler might not find a symbol if you are compiling from the command line. You might simply have forgotten to compile or recompile some other class. For example, if you have classes
  Foo and
  Bar where
  Foo uses
  Bar. If you have never compiled
  Bar and you run
  javac Foo.java, you are liable to find that the compiler can’t find the symbol
  Bar. The simple answer is to compile
  Foo and
  Bar together; e.g.
  javac Foo.java Bar.java or
  javac *.java. Or better still use a Java build tool; e.g. Ant, Maven, Gradle and so on.
  There are some other more obscure causes too … which I will deal with below.
  How do I fix these errors ?
  Generally speaking, you start out by figuring out what caused the compilation error.
  - Look at the line in the file indicated by the compilation error message.
  - Identify which symbol that the error message is talking about.
  - Figure out why the compiler is saying that it cannot find the symbol; see:
  - I could change the inner
  forto
  for (int j = 1; j < 10; j++)– probably correct.
  - I could add a declaration for
  jbefore the inner
  forloop, or the outer
  forloop – possibly correct.
  - I could change
  jto
  iin the inner
  forloop – probably wrong!
  - and so on.
  The point is that you need to understand what your code is trying to do in order to find the right fix.
  Obscure causes
  Here are a couple of cases where the “Cannot find symbol” is seemingly inexplicable … until you look closer.
  - Incorrect dependencies: If you are using an IDE or a build tool that manages the build path and project dependencies, you may have made a mistake with the dependencies; e.g. left out a dependency, or selected the wrong version. If you are using a build tool (Ant, Maven, Gradle, etc), check the project’s build file. If you are using an IDE, check the project’s build path configuration.
  - Cannot find symbol ‘var’: You are probably trying to compile source code that uses local variable type inference (i.e. a
  vardeclaration) with an older compiler or older
  --sourcelevel. The
  varwas introduced in Java 10. Check your JDK version and your build files, and (if this occurs in an IDE), the IDE settings.
  - You are not recompiling: It sometimes happens …
  - An earlier build problem: It is possible that an earlier build failed in a way that gave a JAR file with missing classes. Such a failure would typically be noticed if you were using a build tool. However if you are getting JAR files from someone else, you are dependent on them building properly, and noticing errors. If you suspect this, use
  tar -tvfto list the contents of the suspect JAR file.
  - IDE issues: People have reported cases where their IDE gets confused and the compiler in the IDE cannot find a class that exists … or the reverse situation.
  - This could happen if the IDE has been configured with the wrong JDK version.
  - This could happen if the IDE’s caches get out of sync with the file system. There are IDE specific ways to fix that.
  - This could be an IDE bug. For instance @Joel Costigliola described a scenario where Eclipse did not handle a Maven “test” tree correctly: see this answer. (Apparently that particular bug was been fixed a long time ago.)
  - Android issues: When you are programming for Android, and you have “Cannot find symbol” errors related to
  R, be aware that the
  Rsymbols are defined by the
  context.xmlfile. Check that your
  context.xmlfile is correct and in the correct place, and that the corresponding
  Rclass file has been generated / compiled. Note that the Java symbols are case sensitive, so the corresponding XML ids are be case sensitive too.Other symbol errors on Android are likely to be due to previously mention reasons; e.g. missing or incorrect dependencies, incorrect package names, method or fields that don’t exist in a particular API version, spelling / typing errors, and so on.
  - Hiding system classes: I’ve seen cases where the compiler complains that
  substringis an unknown symbol in something like the following
  String s = ... String s1 = s.substring(1);It turned out that the programmer had created their own version of
  Stringand that his version of the class didn’t define a
  substringmethods. I’ve seen people do this with
  System,
  Scannerand other classes.Lesson: Don’t define your own classes with the same names as common library classes!The problem can also be solved by using the fully qualified names. For example, in the example above, the programmer could have written:
  java.lang.String s = ... java.lang.String s1 = s.substring(1);
  -escapes for other characters.
  What does a “Cannot find symbol” error mean? Answer #2:
  One more example of ‘Variable()) { message = "Everything is fine"; } System.out.println(message);
  What does a “Cannot find symbol” error mean? Answer #3:
  You’ll also get this error if you forget a
  new:
  String s = String();
  versus
  String s = new String();
  because the call without the
  new keyword will try and look for a (local) method called
  String without arguments – and that method signature is likely not defined.
  And one of the simplest solution that might work for you in 99% of the cases is:
  Using IntelliJ
  Select Build->Rebuild Project will solve it.
  Hope this post helped you in understanding what does a “Cannot find symbol” error means.
  Follow Programming Articles for more!