天天看点

Java常见错误列表

java常见错误列表:

找不到符号(symbol)

类x是public的,应该被声明在名为x.java的文件中

缺失类、接口或枚举类型

缺失x

缺失标识符

非法的表达式开头

类型不兼容

非法的方法声明;需要返回类型

数组越界(java.lang.arrayindexoutofboundsexception)

字符越界(java.lang.stringindexoutofboundsexception)

类y中的方法x参数不匹配

缺少return语句

精度损失

在解析时到达了文件结尾

执行不到的语句

变量没被初始化

当你在代码中引用一个没有声明的变量时一般会报这个错误。考虑下面的例子:

1

2

3

4

5

6

7

8

9

10

<code>public</code> <code>class</code> <code>test {</code>

<code></code><code>public</code> <code>static</code> <code>void</code> <code>main(string[] args) {</code>

<code></code><code>int</code> <code>a =</code><code>3</code><code>;</code>

<code></code><code>int</code> <code>b =</code><code>4</code><code>;</code>

<code></code><code>int</code> <code>c =</code><code>20</code><code>;</code>

<code></code><code>average = (a + b + c)/</code><code>5.0</code><code>;</code>

<code></code><code>system.out.println(average);</code>

<code></code><code>}</code>

<code>}</code>

<code>1 error found:</code>

<code>file: test.java &lt;hr&gt;</code>

<code>error: test.java:7: cannot</code><code>find</code>

<code>symbol</code>

<code>symbol : variable average</code>

<code>location: class test</code>

在上面的例子中,变量average没有被声明——也就是说你需要告诉编译器average的类型是什么,例如:

<code>double</code> <code>average = (a + b + c)/</code><code>5.0</code><code>;</code>

此外,当你在代码中引用一个方法但没有在方法名后加上括号时也会报这个错误,加上括号用以表明引用的是个函数,即使当函数没有参数时也不能省略括号。例如:

<code></code><code>my_method;</code>

<code></code><code>public</code> <code>static</code> <code>void</code> <code>my_method() {</code>

<code></code><code>system.out.println(</code><code>"hello, world!"</code><code>);</code>

<code>symbol : variable my_method</code>

在上面的例子中,编译器在main方法中查找名为my_method的变量,实际上,你是想调用一个叫做my_method的方法:

<code></code><code>public</code> <code>class</code> <code>test {</code>

<code></code><code>my_method();</code>

第三种情况,如果你忘记导入你所使用的包时也会出现这个错误。例如,考虑下面这个从用户那里读入一个整数的例子:

<code></code><code>scanner console =</code>

<code>new</code> <code>scanner(system.in);</code>

<code></code><code>int</code> <code>n = console.nextint();</code>

<code>2 errors found:</code>

<code></code><code>file: test.java &lt;hr&gt;</code>

<code></code><code>error: cannot</code>

<code>find</code> <code>symbol</code>

<code></code><code>symbol: class scanner</code>

<code></code><code>location: class test</code>

这里的问题是程序必须导入java.util.scanner(或者java.util.)。否则,编译器不知道scanner是什么类型。当你在处理文件的输入/输出时,如果忘记导入java.util.arrays或者java.io.,也会遇到这个错误。

<code>import</code> <code>java.util.*;</code>

<code>// or --&gt; import java.util.scanner;</code>

最 后,当我们在使用大小敏感的变量名时也会遇到这个错误。java中所有的标识符(identifiers)都是区分大小写的。这就意味着,如果我们声明了 一个名为average的变量,然后在后面用average引用它时,编译器就会报找不到average这个变量的错误。

在一个java程序中,如果类名与文件名不匹配时会报这个错。例如,下面这个foo.java程序:

<code>public</code> <code>class</code> <code>bar {</code>

<code>file: foo.java &lt;hr&gt;</code>

<code>error: class bar is public, should be declared</code><code>in</code>

<code>a</code><code>file</code> <code>named bar.java</code>

由于foo与bar不匹配,这段代码会编译失败。修改这个错误,我们既可以修改类名,也可以修改文件名。

这个错误是一种与大括号有关的错误,一般来说,这个错误发生在程序最后有太多大括号时;例如:

<code></code><code>system.out.println(</code><code>"hello!"</code><code>);</code>

<code>error: class, interface, or enum expected</code>

一 种找出这种错误的方式是正确的缩进代码(因为这种错误总是与大括号有关)。我们可以在dr.java中按组合键ctrl-a(去选中这个程序),然后按 tab键(来正确地缩减代码)。在我们上面的实例代码中,程序的最后有两个大括号,这在一个合法的程序中是不可能出现的。因此,我们仅仅去掉一个大括号就 能够让程序正确的编译。

当编译器检查到代码中缺失字符时会出现”缺失x”这种形式的错误,错误信息会告诉你在哪行缺失了哪个字符,考虑下面的程序:

<code>public</code> <code>class</code> <code>test</code>

<code></code><code>system.out.println(</code><code>"hello, world!"</code><code>)</code>

<code>error: test.java:1:</code><code>'{'</code>

<code>expected</code>

<code>file:.java &lt;hr&gt;</code>

<code>error: test.java:7:</code><code>';'</code>

这个错误信息告诉你在第1行缺失了一个大括号,在第7行缺失了一个分号。解决这种错误很简单——只需把缺失的字符在正确的位置上补上即可。

当把代码写在了方法外时会出现这个错误;这种错误一般也是由大括号引起的。考虑下面的例子:

<code></code><code>system.out.println(</code><code>"world!"</code><code>);</code>

<code>error: &lt;identifier&gt; expected</code>

<code>error: illegal start of</code><code>type</code>

在这种情况下,很明显第一个打印语句应该放在main方法里面,这样才能通过编译。然而,当我们的程序中有多于一个方法并且大括号也不匹配时,这种“缺失标识符”的错误就不容易被发现了:

<code></code><code>system.out.println(</code><code>"hello!"</code><code>);}</code>

<code>3 errors found:</code>

在上面的代码中多了一个大括号,但是因为代码没有正确的缩进,所以很难找出这个错误。这样使得main方法在打印“hello”语句后就结束了,这样打印“world”的语句就变成方法以外的代码了。修改这个错误的方式十分简单——只需要把第三行的大括号删除就可以了:

当编译器遇到一条不合法的语句时会报“非法的表达式开头”这种错误。考虑下面的例子:

11

<code>5 errors found:</code>

<code>error: test.java:6: illegal start of expression</code>

<code>error: test.java:6:</code><code>';'</code>

<code>error: test.java:9: reached end of</code><code>file</code>

<code>while</code> <code>parsing</code>

这里,缺少了一个关闭main方法大括号。由于main方法没有被关闭,编译器把调用my_method方法之后的代码也当作main方法的一部分。然而,后面的代码是public static void my_method() {,很显然,这在一个方法内不合法。

“非 法的表达式开头”这种错误不如我们上面提到的“××缺失”这种信息有帮助。对于这种错误(以及很多其他一些错误),非常有必要检查一下出错代码前面的那几 行。对于上面那个例子,我们只需要在编译器报错的那行前面加上大括号关闭main方法就可以了。重新编译,所有的错误都解决了。

当 你的程序在处理类型相关的问题时会报这个错。我们可以对一些类型进行相互转化,例如,你可以轻松把一个char类型转为int类型,反之亦然;你也可以通 过向上转型把一个double类型转为int类型。但是,你不能把基本类型与像string这样的对象进行相互转换。例如:

<code></code><code>int</code> <code>num =</code><code>"hello, world!"</code><code>;</code>

<code>error: test.java:3: incompatible types</code>

<code>found : java.lang.string</code>

<code>required: int</code>

一般来说,你不能像解决其他一些错误一样解决这种错误。这不是一种语法错误,而是一种关于类型的逻辑错误。把一个string类型转为int类型一般来说都是毫无意义。但是,在一些应用中,你可能需要把string类型转为int类型,比如,当这个字符串代码一个数字时:

<code></code><code>int</code> <code>num =</code><code>"500"</code><code>;</code>

解决这种错误一般采用这样的方法:借助于java中像integer这样的类,这些基本类型的包装类中有能接受字符串类型的参数的方法,这样就把字符串类型转为整型了:

12

13

14

15

16

17

18

19

20

21

22

23

24

25

<code></code><code>int</code> <code>num = integer.parseint(&amp;quot;</code><code>500</code><code>&amp;quot;);</code>

<code>```</code>

<code>但是,这种解决“类型不兼容”错误的方案是一种例外,不是什么规则,因为这种错误一般来自于逻辑上的错误。</code>

<code>&lt;a name=</code><code>"invalid-method"</code><code>/&gt;&lt;/a&gt;</code>

<code>###</code><code>8</code><code>. 非法的方法声明;需要返回类型</code>

<code>在java中的每个方法都要求明确的声明返回类型,即使这个方法什么也不返回,也要用</code><code>void</code><code>进行标识,就像main方法那样。</code>

<code>当一个方法没有声明返回类型时,会出现这种错误:</code>

<code>&amp;lt;pre</code><code>class</code><code>=&amp;quot;brush: java; gutter:</code><code>true</code><code>; first-line:</code><code>1</code><code>; highlight: []; html-script:</code><code>false</code><code>&amp;quot;&amp;gt;</code>

<code></code><code>int</code> <code>x = getvalue();</code>

<code></code><code>system.out.println(x);</code>

<code></code><code>public</code> <code>static</code> <code>getvalue() {</code>

<code></code><code>return</code> <code>10</code><code>;</code>

<code>error: test.java:7: invalid method declaration;</code>

<code>return</code> <code>type</code>

<code>required</code>

解决这种问题,在方法声明处加上合适的返回类型即可:

<code></code><code>public</code> <code>static</code> <code>int</code> <code>getvalue() {</code>

当你使用不合法的索引访问数组时会报数组越界这种错误,数组arr的合法错误范围是[0, arr.length-1];当你访问这之外的索引时会报这个错。例如:

<code></code><code>int</code><code>[] arr = {</code><code>1</code><code>,</code><code>2</code><code>,</code><code>3</code><code>};</code>

<code></code><code>for</code> <code>(</code><code>int</code> <code>i =</code>

<code>0</code><code>; i &lt;= arr.length; i++) {</code>

<code></code><code>system.out.println(arr[i]);</code>

<code>java.lang.arrayindexoutofboundsexception: 3</code>

<code>at test.main(test.java:5)</code>

<code>at sun.reflect.nativemethodaccessorimpl.invoke0(native method)</code>

<code>at sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57)</code>

<code>at sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43)</code>

<code>at java.lang.reflect.method.invoke(method.java:606)</code>

<code>at edu.rice.cs.drjava.model.compiler.javaccompiler.runcommand(javaccompiler.java:272)</code>

这种错误很像我们下面即将说的字符串索引越界,这种错误的错误信息后面部分与错误不大相关。但是,第1行就告诉我们错误的原因是数组越界了,在我们上面的例子,非法的索引值是3,下面一行的错误信息告诉你错误发生在test类的第5行上,在main方法之内。

在上面的例子中,因为我们循环过多导致出现这个错误,循环索引i最大可以为4,而4超过了数组的长度,因此越界了。相反,i的上界应该使用&lt;或者相同效果的语句来界定。

<code>0</code><code>; i &lt; arr.length; i++) {</code>

当处理数组越界时,打印出遍历数组的索引十分有帮助,这样我们就能够跟踪代码找到为什么索引达到了一个非法的值。

当你在程序中去访问一个字符串的非法索引时会报字符串索引越界这个错误。一个string的合法索引范围是[0,str.leng()-1];当你访问这之外的索引时会报这个错。例如:

<code></code><code>string str =</code><code>"hello, world!"</code><code>;</code>

<code></code><code>string a = str.substring(-</code><code>1</code><code>,</code><code>3</code><code>);</code>

<code></code><code>string b = str.charat(str.length());</code>

<code></code><code>string c = str.substring(</code><code>0</code><code>,</code><code>20</code><code>);</code>

<code>java.lang.stringindexoutofboundsexception: string index out of range: -1</code>

<code></code><code>at java.lang.string.substring(unknown source)</code>

<code></code><code>at test.main(test.java:5)</code>

<code></code><code>at sun.reflect.nativemethodaccessorimpl.invoke0(native method)</code>

<code></code><code>at sun.reflect.nativemethodaccessorimpl.invoke(unknown source)</code>

<code></code><code>at sun.reflect.delegatingmethodaccessorimpl.invoke(unknown source)</code>

<code></code><code>at java.lang.reflect.method.invoke(unknown source)</code>

<code></code><code>at edu.rice.cs.drjava.model.compiler.javaccompiler.runcommand(javaccompiler.java:271)</code>

这 种错误的错误信息后面部分与错误不大相关。但是,第1行就说明了错误的地方是字符串的索引,在我们这个例子,非法的索引是-1,下面一行错误信息告诉我们 这个错误是在执行substring方法时抛出的,发生错误的位置是test类的第5行。这种与错误相关的程序轨迹告诉我们程序是在调用哪个方式时出的 错,这样我们就能追踪代码,并最终改正它。

值得注意的是,上面程序中的a,b,c都会抛出这种错误,但是程序在遇到第一个错误时就被迫终止了。

这不是编译时的错误,而是运行时的错误。换句话说,编译器能正确编译这段程序因为它只是在逻辑上有错,此外,在程序运行之前,我们也没法预料是否会有错误发生。解决这种错误,我们需要改正程序的逻辑来保证没有地方访问非法的索引。

当你在调用函数时参数数量或顺序不对时会报这个错误。例如,考虑下面的程序:

<code></code><code>mymethod(</code><code>1.0</code><code>,</code><code>2</code><code>,</code><code>"hello!"</code><code>);</code>

<code></code><code>public</code> <code>static</code> <code>void</code> <code>mymethod(</code><code>double</code>

<code>d, string s,</code><code>int</code>

<code>x) {</code>

<code></code><code>system.out.println(s +</code>

<code>" "</code> <code>+ d +</code><code>" "</code>

<code>+ x);</code>

<code>error: method mymethod</code><code>in</code>

<code>class test cannot be applied to given types;</code>

<code></code><code>required: double,java.lang.string,int</code>

<code></code><code>found: double,int,java.lang.string</code>

<code></code><code>reason: actual argument int cannot be converted to java.lang.string by method invocation conversion</code>

这种错误的错误信息非常有帮助。“required”这一行错误信息告诉我们方法的参数是什么,方法的参数列表在这后面。在上面的例子中,mymethod方法的参数先后顺序应该是double类型、string类型,最后是一个int类型的变量。

错误信息的下一行(found开头的这一行)告诉我们程序在调用这个方法时用了什么样的参数。在上面的例子中,是一个double类型,一个int类型,最后是一个string类型的变量,很显然顺序是不对的。

解决这种错误,我们需要保证方法的参数个数和类型与函数声明时都一致才行。

<code></code><code>mymethod(</code><code>1.0</code><code>,</code><code>"hello!"</code><code>,</code>

<code>2</code><code>);</code>

当你声明一个方法有返回值但是没有写return语句时会报这个错误。例如:

<code></code><code>int</code> <code>x = twice(</code><code>5</code><code>);</code>

<code></code><code>public</code> <code>static</code> <code>int</code> <code>twice(</code><code>int</code>

<code></code><code>int</code> <code>value =</code><code>2</code> <code>* x;</code>

<code>error: test.java:9: missing</code><code>return</code>

<code>statement</code>

我们通过函数声明告知编译器twice方法会返回一个int值,但是我们没有写return语句:

<code></code><code>return</code> <code>value;</code>

在某些if条件句中,编译器也会认为函数没有返回值。像下面这个例子:

<code></code><code>int</code> <code>x = absval(-</code><code>5</code><code>);</code>

<code></code><code>public</code> <code>static</code> <code>int</code> <code>absval(</code><code>int</code>

<code></code><code>if</code> <code>(x &lt;</code><code>0</code><code>) {</code>

<code></code><code>return</code> <code>-x;</code>

<code></code><code>if</code> <code>(x &gt;=</code><code>0</code><code>) {</code>

<code></code><code>return</code> <code>x;</code>

<code>error: test.java:15: missing</code><code>return</code>

避免这种错误,我们可以选择使用else语句(就像我们在变量没被初始化一样),或者我们可以不用第二个if语句,因为我们知道,如果程序能够执行到这个地方,程序就可以直接返回x了:

当你把信息保存到一个变量中,而信息量超过了这个变量的所能容纳的能力时会报这个错。最常见的例子是把double类型赋值给int类型。

<code></code><code>int</code> <code>pi =</code><code>3.14159</code><code>;</code>

<code></code><code>system.out.println(</code><code>"the value of pi is: "</code>

<code>+ pi);</code>

<code>error: test.java:3: possible loss of precision</code>

<code>found : double</code>

这个错误发生的原因是计算机在存储double类型时所需的空间是int类型的两倍。如果你不在乎精度的损失,你可以通过上转型的方法来告知编译器:

<code></code><code>int</code> <code>pi = (</code><code>int</code><code>)</code><code>3.14159</code><code>;</code>

现在编译器不会报错了,但是pi这个变量由于进行了取整,最终值为3。

当你没有用大括号关闭你的程序时会出现这个错误。错误信息明确的指出编译器在没有明确程序该结束时就到达了文件的结尾。例如:

<code>1</code> <code>error found:</code>

<code>error: test.java:</code><code>9</code><code>: reached end of file</code><code>while</code> <code>parsing</code>

解决这个错误,我们只需要在最后加上关闭程序的大括号(“}”)即可。有时仅仅在文件末尾缺少了一个大括号,但也有可能是在程序的中间少写或多写了大括号的缘故。

一种调试的方法是用快捷键ctrl-a + tab来正确的缩减你的代码。由于程序的问题与大括号有关,这样代码就不能够正确的缩进。找到程序中第一个缩进不正确的地方,这就是错误产生的地方。

一旦大括号正确的匹配上,编译器就不会报错了:

当编译器检测到某些语句在整个程序流程中不可能被执行到时会报这个错。这个错误经常是由return或break后的语句所导致的。例如:

<code></code><code>int</code> <code>value = twice(</code><code>5</code><code>);</code>

<code></code><code>system.out.println(value);</code>

<code></code><code>int</code> <code>twice =</code><code>2</code> <code>* x;</code>

<code></code><code>return</code> <code>twice;</code>

<code></code><code>system.out.println(</code><code>"returning "</code>

<code>+ twice);</code>

<code>error: test.java:10: unreachable statement</code>

<code>error: test.java:11: missing</code><code>return</code>

编译器报了两个错:一个是说system.out.println(“returning ” + twice);这一行不可能被法执行到,另一个错误是因为编译器假设可以执行print语句,这样的话我们在它之后也应该有个return语句,但是程序中没有,所以报这个错。

解决这个错误,我们可以把print语句放到return的前面,这样程序就能执行了:

当你在程序中去引用一个没有被初始化的变量时会报这个错。下面看一个非常简单的例子:

<code></code><code>int</code> <code>x =</code><code>2</code><code>;</code>

<code></code><code>int</code> <code>y;</code>

<code></code><code>system.out.println(x + y);</code>

<code>error: test.java:</code><code>5</code><code>: variable y might not have been initialized</code>

在程序中你没有告知编译器y的值,所以y不能被打印,y需要像x一样被初始化以后才能使用。

在一些更复杂的情形下,if语句可能导致变量没有被初始化。例如:

<code></code><code>int</code> <code>x;</code>

<code></code><code>boolean</code> <code>setx =</code><code>false</code><code>;</code>

<code></code><code>if</code> <code>(setx) {</code>

<code></code><code>x =</code><code>10</code><code>;</code>

<code>error: test.java:8: variable x might not have been initialized</code>

这里很明显,x将不能被正确的初始化,因此编译器报错。但是,在一些情况下虽然我们能够很清楚的知道变量能够被初始化,但是编译器不能和我们一样推测出变量是否会被初始化,例如:

<code></code><code>boolean</code> <code>settoten =</code><code>false</code><code>;</code>

<code></code><code>if</code> <code>(settoten) {</code>

<code></code><code>if</code> <code>(!settoten) {</code>

<code></code><code>x =</code><code>0</code><code>;</code>

<code>error: test.java:14: variable x might not have been initialized</code>

很明显,x一定会被两个if语句中的任意一个赋值,但是编译器并不能推测出(译者注:需要在运行时才能知道),一种修改这个错误的方式是使用else语句。当使用else语句时,编译器就有最够的证据推测出x将被初始化:

<code></code><code>}</code><code>else</code>

<code>{</code>