“不到符号”或“⽆法解析符号”错误是什么意思?
本⽂翻译⾃:
Please explain the following about "Cannot find symbol" and "Cannot resolve symbol" errors: 请解释以下有关“不到符网页版html编辑器怎么编辑
请解释以下有关“不到符
号”和“⽆法解析符号”错误的信息:
What does 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.该问题旨在对Java 中的这些常见编译错误进⾏全⾯的问答。
#1楼
#2楼
0. Is there any difference between the two errors? 0.两种错误之间有区别吗?
Not really. 并不是的。
并不是的。 "Cannot find symbol" and "Cannot resolve symbol" mean the same thing. “不到符号”和“⽆法解析
“不到符号”和“⽆法解析
⼀些Java编译器使⽤⼀个短语,⽽另⼀些则符号”含义相同。 Some Java compilers use one phrase, and some the other one. ⼀些Java编译器使⽤⼀个短语,⽽另⼀些则符号”含义相同。
使⽤。
1. What does a "Cannot find symbol" error mean? 1.“不到符号”错误是什么意思?
Firstly, it is a compilation error 1 . ⾸先,它是
⾸先,它是编译错误1 。
。 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. 这意味着,
这意味着, ⽆论有在Java源代码中出现问题, 或者是你正在编译的⽅式有问题。
Your Java source code consists of the following things: 您的Java源代码包含以下内容:
您的Java源代码包含以下内容:
Keywords: like true , false , class , while , and so on. 关键字:像
关键字:像true , false , class , while等等。
Literals: like 42 and 'X' and "Hi mum!" ⽂字:例如
数据库设计文档评审⽂字:例如42和'X'以及"Hi mum!". 。。
Operators and other non-alphanumeric tokens: like + , = , { , and so on. 运算符和其他⾮字母数字标记:像
运算符和其他⾮字母数字标记:像+ , = , {等等。
Identifiers: like Reader , i , toString , processEquibalancedElephants , and so on. 标识符:如
标识符:如Reader , i , toString ,processEquibalancedElephants等。
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. 您的代码似乎是指编译器⽆法理解的内容。
您的代码似乎是指编译器⽆法理解的内容。
2. What can cause a "Cannot find symbol" error? 2.什么会导致“不到符号”错误?
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; 也许您拼错了名字;
也许您拼错了名字; ie StringBiulder instead of StringBuilder .
。 Java cannot and will not attempt to compensate for bad spelling or typing 即StringBiulder⽽不是StringBuilder 。
errors. Java⽆法也不会尝试弥补拼写错误或输⼊错误。
Java⽆法也不会尝试弥补拼写错误或输⼊错误。
Perhaps you got the case wrong; 也许你错了。
也许你错了。 ie stringBuilder instead of StringBuilder . 即即stringBuilder⽽不
所有Java标识符均区分⼤⼩写。
。 All Java identifiers are case sensitive. 所有Java标识符均区分⼤⼩写。
是StringBuilder 。
Perhaps you used underscores inappropriately; 也许您不恰当地使⽤了下划线;
也许您不恰当地使⽤了下划线; ie mystring and my_string are different.
即mystring和my_string不同。
不同。 (If you stick to the Java style rules, you will be largely protected from this mistake ...)(如果您坚持使⽤Java样式规则,则将在很⼤程度上避免出现此错误...)
Perhaps you are trying to use something that was declared "somewhere else"; 也许您正在尝试使⽤被声明为“其他地
也许您正在尝试使⽤被声明为“其他地
即在与您隐式告诉编译器⽅”的东西; ie 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 (ie has not been declared) in the type you are using; 也许您正在尝试引⽤所使⽤的类型中不存在(即尚未声明)的⽅法或字段;
也许您正在尝试引⽤所使⽤的类型中不存在(即尚未声明)的⽅法或字段; eg "someString".push()2 . 例例如"someString".push()2 。
Perhaps you are trying to use a method as a field, or vice versa; 也许您正在尝试将⽅法⽤作字段,反之亦然;
accesssql查询语句
也许您正在尝试将⽅法⽤作字段,反之亦然; eg
例如"someString".length或someArray.length() 。
"someString".length or someArray.length() . 例如
Perhaps you are mistakenly operating on an array rather than array element; 也许您是错误地对数组⽽不是数组元素进
也许您是错误地对数组⽽不是数组元素进例如
⾏操作。
⾏操作。 eg 例如
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: 也许您忘记了以下
也许您忘记了以下new :
String s = String(); // should be 'new String()'
For cases where type or instance doesn't appear to have the member 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; 也许您输⼊了错误的类型;
例如
也许您输⼊了错误的类型; eg due to IDE completion or auto-correction. 例如由于IDE完成或⾃动更正。
Perhaps you are using (compiling against) the wrong version of an API. 也许您正在使⽤(针对)错误版本的API。
也许您正在使⽤(针对)错误版本的API。
Perhaps you forgot to cast your object to an appropriate subclass. 也许您忘记了将对象转换为适当的⼦类。
也许您忘记了将对象转换为适当的⼦类。
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 . 例如,也许您“星标”了导⼊的
例如,也许您“星标”了导⼊的java.io.* ,然后尝
也许您打算写File
。 Or maybe you meant to write File ... which is a class in java.io . 也许您打算写
试使⽤Files类...,它在java.nio⽽不是java.io 。
...这是java.io⼀个类。
Here is an example of how incorrect variable scoping can lead to a "Cannot find symbol" error: 这是⼀个⽰例,说明变量作⽤
这是⼀个⽰例,说明变量作⽤域范围不正确会如何导致“不到符号”错误:
conversion转化词例子List<String> strings = ...
for (int i = 0; i < strings.size(); i++) {
if ((i).equalsIgnoreCase("fnord")) {
break;
}
}
if (i < strings.size()) {
...
}
This will give a "Cannot find symbol" error for i in the if statement. 这将使“⽆法到符号”错误
声明。 Though we
这将使“⽆法到符号”错误i在if声明。
previously declared i , that declaration is only in scope for the for statement and its body. 尽管我们之前曾声明过
尽管我们之前曾声明过i ,但该声明 。 The reference to i in the if statement cannot see that declaration of i . 在在if语句中对i的引⽤看仅在for语句及其主体的范围内 。
不到i声明。
声明。 It is out of scope . 它它超出范围 。
(An appropriate correction here might be to move the if statement inside the loop, or to declare i before the start of the loop.) (这⾥的适当修正可能是将
(这⾥的适当修正可能是将if语句移⼊循环,或在循环开始之前声明i 。)
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. 这会给你⼀个编
译错误的
这会给你⼀个编译错误的println电话,
但是(我听到你说)我确实宣布了!
说i⽆法到。
⽆法到。 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 . Java语⾔语法在该上下⽂中将分号定义为
。 The empty statement
Java语⾔语法在该上下⽂中将分号定义为空语句 。
then becomes the body of the for loop. 然后,空语句将成为
循环的主体。 So that code actually means this: 因此该代码实际
因此该代码实际
然后,空语句将成为for循环的主体。
上意味着:
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 s
tatement is out of scope in the block.{ ... }块不是for循环的主体,因此for语句中i的先前声明超出了该块的范围 。
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. 尽管与先前的声明中,
尽管与先前的声明中, tmp在tmp(...)的表达是
编译器将寻⼀种名为tmp的⽅法,但不到。
错误的。 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. 先前声明的
先前声明的tmp在变量的名称空间中,⽽不在⽅法的名称空间中。
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 com
mand 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 您有Foo和Bar类,其中Foo使⽤Bar 。
the compiler can't find the symbol Bar . 如果您从未编译过
如果您从未编译过Bar并且运⾏了javac Foo.java ,则很可能会发现编译器不到Bar的符
简单的答案是将Foo和Bar⼀起编译。
⼀起编译。 eg javac Foo.java Bar.java or 号。 The simple answer is to compile Foo and Bar together; 简单的答案是将
号。
或者最好还是使⽤Java构建⼯具;
。 Or better still use a Java build tool; 或者最好还是使⽤Java构建⼯具;javac *.java . 例如
例如javac Foo.java Bar.java或javac *.java 。
eg Ant, Maven, Gradle and so on. 例如Ant,Maven,Gradle等。
例如Ant,Maven,Gradle等。
There are some other more obscure causes too ... which I will deal with below. 还有⼀些其他更晦涩的原因...我将在下⾯处理。
还有⼀些其他更晦涩的原因...我将在下⾯处理。
3. How do I fix these errors ? 3.如何解决这些错误?
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
弄清楚编译器为什么说不到符号; 为什么 ?
above! 往上看!
properties是什么文件往上看!
Then you think about what your code is supposed to be saying. 然后,您
⼀下您的代码应该说什么。 Then finally you
然后,您考虑⼀下您的代码应该说什么。
work out what correction you need to make to your source code to do what you want. 然后,最后您确定需要对源代码进⾏哪
然后,最后您确定需要对源代码进⾏哪些更正以执⾏所需的操作。
eclipse怎么改字体Note that not every "correction" is correct. 请注意,并⾮每个“更正”都是正确的。
请注意,并⾮每个“更正”都是正确的。 Consider this: 考虑⼀下:
考虑⼀下:
for (int i = 1; i < 10; i++) {
for (j = 1; j < 10; j++) {
...
}
}
Suppose that the compiler says "Cannot find symbol" for j . 假设编译器为
说“不到符号”。 There are many ways I could
假设编译器为j说“不到符号”。
"fix" that: 我可以通过多种⽅式“修复”该问题:
我可以通过多种⽅式“修复”该问题:
I could change the inner for to for (int j = 1; j < 10; j++) - probably correct. 我可以将
我可以将for的内部变量更改for (int j = 1; j < 10; j++) -可能是正确的。
I could add a declaration for j  before the inner for  loop, or the outer for  loop - possibly correct. 我可以在内部我可以在内部for 循环或外部for 循环之前添加j 的声明-可能是正确的。
I could change j  to i  in the inner for  loop - probably wrong! 我可以在内部我可以在内部for 循环中将j 更改为i 可能错了!
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. 关键是您关键是您需要了解您的代码正在尝试做什么才能到正确的修复程序。
4. Obscure causes  4.原因不明
Here are a couple of cases where the "Cannot find symbol" is seemingly inexplicable ... until you look closer. 在⼀些情况在⼀些情况下,“不到符号”似乎⽆法解释……直到您仔细观察。
1. 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; 不正确的依赖关系 :如果您使⽤的是IDE或管理构建路径和项⽬依赖不正确的依赖关系 :如果您使⽤的是IDE或管理构建路径和项⽬依赖关系的构建⼯具,则可能是错误的依赖关系;关系的构建⼯具,则可能是错误的依赖关系; eg left out a dependency, or selected the wrong version. 例如遗漏了⼀个依例如遗漏了⼀个依赖项,或者选择了错误的版本。赖项,或者选择了错误的版本。 If you are using a build tool (Ant, Mave
n, Gradle, etc), check the project's build file. 如果如果您使⽤的是构建⼯具(Ant,Maven,Gradle等),请检查项⽬的构建⽂件。您使⽤的是构建⼯具(Ant,Maven,Gradle等),请检查项⽬的构建⽂件。 If you are using an IDE, check the project's build path configuration. 如果使⽤的是IDE,请检查项⽬的构建路径配置。如果使⽤的是IDE,请检查项⽬的构建路径配置。
2. You are not recompiling : It sometimes happens that new Java programmers don't understand how the Java tool chain works, or haven't implemented a repeatable "build process"; 您⽆需重新编译 :有时候,新Java程序员可能不了解Java⼯您⽆需重新编译 :有时候,新Java程序员可能不了解Java⼯具链的⼯作⽅式,或者没有实现可重复的“构建过程”;具链的⼯作⽅式,或者没有实现可重复的“构建过程”; eg using an IDE, Ant, Maven, Gradle and so on. 例如使⽤例如使⽤IDE,Ant,Maven,Gradle等。IDE,Ant,Maven,Gradle等。 In such a situation, the programmer can end up chasing his tail looking for an illusory error that is actually caused by not recompiling the code properly, and the like ... 在这种情况下,程序员最终可能会追尾在这种情况下,程序员最终可能会追尾寻⼀个虚幻的错误,该错误实际上是由于未正确地重新编译代码⽽导致的,等等。
3. An earlier build problem : It is possible that an earlier build failed in a way that gave a JAR file with missing classes. 较较早的构建问题 : 较早的构建可能会失败,从⽽导致JAR⽂件缺少类。早的构建问题 : 较早的构建可能会失败,从⽽导致JAR⽂件缺少类。 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. 但是,如果要从其他⼈那⾥获取JAR⽂件,则取决于但是,如果要从其他⼈那⾥获取JAR⽂件,则取决于它们是否可以正确构建并注意到错误。是否可以正确构建并注意到错误。 If you suspect this, use tar -tvf  to list the contents of the suspect JAR file. 如果您怀疑如果您怀疑这⼀点,请使⽤tar -tvf 列出可疑JAR⽂件的内容。
4. 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. IDE问题 :⼈们报告说,他们的IDE感到困惑,并且IDE中的编译器不到存在的IDE问题 :⼈们报告说,他们的IDE感到困惑,并且IDE中的编译器不到存在的类……或相反的情况。
1. This could happen if the IDE has been configured with the wrong JDK version. 如果IDE配置了错误的JDK版本,则如果IDE配置了错误的JDK版本,则可能会发⽣这种情况。
2. This could happen if the IDE's caches get out of sync with the file system. 如果IDE的缓存与⽂件系统不同步,则可如果IDE的缓存与⽂件系统不同步,则可能发⽣这种情况。能发⽣这种情况。 There are IDE specific ways to fix that. 有特定于IDE的⽅法可以解决此问题。有特定于IDE的⽅法可以解决此问题。
3. This could be an IDE bug. 这可能是⼀个IDE错误。这可能是⼀个IDE错误。 For instance @Joel Costigliola describes a scenario where Eclipse does not handle a Maven "test" tree correctly: . 例如,@ Joel Costigliola描述了Eclipse⽆法正确处理例如,@ Joel Costigliola描述了Eclipse⽆法正确处理
Maven“测试”树的情况: Maven“测试”树的情况: 。。
5. Android issues : When you are programming for Android, and you have "Cannot find symbol" errors related to R  , be aware that the R  symbols are defined by l  file. Android问题 :当您使⽤Android进⾏编程时,遇到与Android问题 :当您使⽤Android进⾏编程时,遇到与R 相关的“不到符号”错误,请注意, R 符号是由l ⽂件定义的。⽂件定义的。 Check that l  file is correct and in the correct place, and that the corresponding R  class file has been generated / compiled. 检查您的检查您的l ⽂件是正确的并且在正确的位置,并且已经⽣成/编译了相应的R 类⽂件。类⽂件。 Note that the Java symbols are case sensitive, so the corresponding XML ids are be case sensitive too. 请注意,Java符号区分⼤⼩写,因此相应的XML ID也区分⼤⼩写。请注意,Java符号区分⼤⼩写,因此相应的XML ID也区分⼤⼩写。

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。