Java常见错误列表:
找不到符号(symbol)
类X是public的,应该被声明在名为X.java的文件中
缺失类、接口或枚举类型
缺失X
缺失标识符
非法的表达式开头
类型不兼容
非法的方法声明;需要返回类型
数组越界(java.lang.ArrayIndexOutOfBoundsException)
字符越界(java.lang.StringIndexOutOfBoundsException)
类Y中的方法X参数不匹配
缺少return语句
精度损失
在解析时到达了文件结尾
执行不到的语句
变量没被初始化
当你在代码中引用一个没有声明的变量时一般会报这个错误。考虑下面的例子:
12345678910 | public class Test { public static void main(String[] args) { int a = 3 ; int b = 4 ; int c = 20 ; average = (a + b + c)/ 5.0 ; System.out.PRintln(average); } } |
12345 | 1 error found: File: Test.java <hr> Error: Test.java:7: cannot find symbol symbol : variable average location: class Test |
在上面的例子中,变量average没有被声明——也就是说你需要告诉编译器average的类型是什么,例如:
1 | double average = (a + b + c)/ 5.0 ; |
此外,当你在代码中引用一个方法但没有在方法名后加上括号时也会报这个错误,加上括号用以表明引用的是个函数,即使当函数没有参数时也不能省略括号。例如:
123456789 | public class Test { public static void main(String[] args) { my_method; } public static void my_method() { System.out.println( "Hello, world!" ); } } |
12345 | 1 error found: File: Test.java <hr> Error: Test.java:7: cannot find symbol symbol : variable my_method location: class Test |
在上面的例子中,编译器在main方法中查找名为my_method的变量,实际上,你是想调用一个叫做my_method的方法:
123456789 | public class Test { public static void main(String[] args) { my_method(); } public static void my_method() { System.out.println( "Hello, world!" ); } } |
第三种情况,如果你忘记导入你所使用的包时也会出现这个错误。例如,考虑下面这个从用户那里读入一个整数的例子:
123456 | public class Test { public static void main(String[] args) { Scanner console = new Scanner(System.in); int n = console.nextInt(); } } |
123456789 | 2 errors found: File: Test.java <hr> Error: cannot find symbol symbol: class Scanner location: class Test File: Test.java <hr> Error: cannot find symbol symbol: class Scanner location: class Test |
这里的问题是程序必须导入java.util.Scanner(或者java.util.)。否则,编译器不知道Scanner是什么类型。当你在处理文件的输入/输出时,如果忘记导入java.util.Arrays或者java.io.,也会遇到这个错误。
1234567 |
学习交流
热门图片
猜你喜欢的新闻
新闻热点 2019-10-23 09:17:05
2019-10-21 09:20:02
2019-10-21 09:00:12
2019-09-26 08:57:12
2019-09-25 08:46:36
2019-09-25 08:15:43
疑难解答 |