在开发Java软件时可能会遇到许多类型的错误,但大多数是可以避免的。为此我们罗列了50个最常见的Java编码错误,其中包含代码示例和教程,以帮助大家解决常见的编码问题。

编译器错误

当Java软件代码通过编译器运行时,会创建编译器错误消息。谨记编译器可能会针对一个错误抛出许多错误消息。所以得修复第一个错误并重新编译。这样做可以解决很多问题。

1.“…Expected”

当代码中缺少某些东西时,会发生此错误。通常是因为缺少分号或右括号。

private static double volume(String solidom, double alturam, double areaBasem, double raiom) {double vol;    if (solidom.equalsIgnoreCase("esfera"){
        vol=(4.0/3)*Math.pi*Math.pow(raiom,3);
    }    else {        if (solidom.equalsIgnoreCase("cilindro") {
            vol=Math.pi*Math.pow(raiom,2)*alturam;
        }        else {
            vol=(1.0/3)*Math.pi*Math.pow(raiom,2)*alturam;
        }
    }    return vol;
}

通常,此错误消息不会精确确定问题的确切位置。为了找到错误:

确保所有的左括号都有一个对应的右括号。

在查看指示的Java代码行之前顺便看看行。此Java软件错误没有被编译器注意到,直到在代码中更深层次地查看。

有时一个字符,如一个左括号,并不应该放在Java代码的原先位置。所以开发人员没有放置一个右括号来平衡括号。

2.“Unclosed String Literal”

如果字符串文字结束时没有引号的话,会创建“Unclosed String Literal”的错误消息,并且消息将显示在与错误相同的行上。(@DreamInCode)Literal是值的源代码。

public abstract class NFLPlayersReference {    private static Runningback[] nflplayersreference;    private static Quarterback[] players;    private static WideReceiver[] nflplayers;    public static void main(String args[]){
    Runningback r = new Runningback("Thomlinsion");
    Quarterback q = new Quarterback("Tom Brady");
    WideReceiver w = new WideReceiver("Steve Smith");
    NFLPlayersReference[] NFLPlayersReference;
        Run();// {
        NFLPlayersReference = new NFLPlayersReference [3];
        nflplayersreference[0] = r;
        players[1] = q;
        nflplayers[2] = w;            for ( int i = 0; i < nflplayersreference.length; i++ ) {
            System.out.println("My name is " + " nflplayersreference[i].getName());
            nflplayersreference[i].run();
            nflplayersreference[i].run();
            nflplayersreference[i].run();
            System.out.println("NFL offensive threats have great running abilities!");
        }
    }
    private static void Run() {
        System.out.println("Not yet implemented");
    }     
}

通常。这种情况发生在:

字符串文字不以引号结尾。这很容易纠正,加上所需的引号,闭合字符串文字即可。

字符串文字扩展超过一行。长字符串文字可以分解成字符串文字,并加上加号(“+”)。

作为字符串文字一部分的引号不通过反斜杠(“\”)进行转义。3.“Illegal Start of an Expression”

出现“Illegal Start of an Expression”错误的原因有很多。因此名列不太有用的错误消息之一。有些开发者甚至认为这是由坏的代码造成的。

通常,创建表达式是为了生成新的值或为变量分配值。编译器期望找到表达式,但因为语法不符合预期而找不到表达式。(@StackOverflow)在这些语句中就可以发现此错误。

} // ADD IT HERE
       public void newShape(String shape) {        switch (shape) {            case "Line":
                Shape line = new Line(startX, startY, endX, endY);
            shapes.add(line);            break;                case "Oval":
            Shape oval = new Oval(startX, startY, endX, endY);
            shapes.add(oval);            break;            case "Rectangle":
            Shape rectangle = new Rectangle(startX, startY, endX, endY);
            shapes.add(rectangle);            break;            default:
            System.out.println("ERROR. Check logic.");
        }
        }
    } // REMOVE IT FROM HERE
    }

4.“Cannot Find Symbol”

这是一个非常常见的问题,因为Java中的所有标识符都需要在被使用之前进行声明。 发生错误的原因在于编译代码时,编译器不明白标识符的含义。

50个常见的 Java 错误及避免方法_Java

可能收到“Cannot Find Symbol”信息的原因有很多:

  • 标识符声明时的拼写可能与在代码中使用时的拼写不同。

  • 变量从未被声明。

  • 变量使用时与声明未在同一范围内。

  • 类没有导入。

点击阅读有关“Cannot Find Symbol”错误的详细讨论以及产生此问题的代码示例。(@StackOverflow)

5.“Public Class XXX Should Be in File”

当XXX类和Java程序文件名不匹配时,会发生“Public Class XXX Should Be in File”消息。只有当类和Java文件相同时,代码才会被编译。(@coderanch):

package javaapplication3;  
  public class Robot {  
        int xlocation;  
        int ylocation;  
        String name;  
        static int ccount = 0;  
        public Robot(int xxlocation, int yylocation, String nname) {  
            xlocation = xxlocation;  
            ylocation = yylocation;  
            name = nname;  
            ccount++;         
        } 
  }  public class JavaApplication1 { 
    public static void main(String[] args) {  
        robot firstRobot = new Robot(34,51,"yossi");  
        System.out.println("numebr of robots is now " + Robot.ccount);  
    }
  }

为解决这个问题:

  • 类和文件命名相同。

  • 确保两名称的情况一致。

点击查阅“Public Class XXX Should Be in File”错误的示例。(@StackOverflow)

6.“Incompatible Types”

“Incompatible Types”是指定语句尝试将变量与类型表达式配对时发生的逻辑错误。 通常会在代码尝试将文本字符串放入到整型中时出现,反之亦然。这并非Java语法错误。(@StackOverflow)

test.java:78: error: incompatible typesreturn stringBuilder.toString();
                             ^required: int
found:    String1 error

当编译器给出“Incompatible Types”消息时,就不是简单修复可以摆平的了:

  • 有可以转换类型的函数。

  • 开发人员可能需要更改代码预期的功能。

查看此例子,里面说明了如何试着将一个字符串分配给一个整数,出现“Incompatible Types”消息。(@StackOverflow)

7.“Invalid Method Declaration; Return Type Required”

此Java软件错误消息表示方法签名中未明确声明方法的返回类型。

public class Circle{    private double radius;    public CircleR(double r)
    {
        radius = r;
    }    public diameter()
    {       double d = radius * 2;       return d;
    }
}

有若干途径会触发“Invalid Method Declaration; Return Type Required” 错误:

忘记声明类型

如果方法没有返回值,那么需要将“void”声明为方法签名中的类型。

构造函数名称不需要声明类型。 但是,如果构造函数名称中存在错误,那么编译器将会把构造函数视为没有指定类型的方法。

查看此说明构造函数命名如何触发“Invalid Method Declaration; Return Type Required” 问题的例子。(@StackOverflow)

8.“Method <X> in Class <Y> Cannot Be Applied to Given Types”

此Java软件错误消息是比较有用的错误消息之一。 它解阐明了方法签名正在调用错误的参数。

RandomNumbers.java:9: error: method generateNumbers in class RandomNumbers cannot be applied to given types;generateNumbers();
required: int[]
found:generateNumbers();
reason: actual and formal argument lists differ in length

调用的方法期待方法声明中定义的某些参数。检查方法声明并仔细调用,以确保它们是兼容的。

查看此讨论,里面说明了Java软件错误消息如何识别在方法声明和方法调用中由参数创建的不兼容性。(@StackOverflow)

9.“Missing Return Statement”

当一个方法没有返回语句时,就会出现“Missing Return Statement”的消息。每个返回值(非void类型)的方法都必须有一个字面上返回该值的语句,以便在方法之外调用它。

public String[] OpenFile() throws IOException {    Map<String, Double> map = new HashMap();
    FileReader fr = new FileReader("money.txt");
    BufferedReader br = new BufferedReader(fr);    try{        while (br.ready()){            String str = br.readLine();            String[] list = str.split(" ");
            System.out.println(list);               
        }
    }   catch (IOException e){
        System.err.println("Error - IOException!");
    }
}

编译器抛出“Missing Return Statement”消息的原因有若干:

  • 返回语句由于错误被省略。

  • 该方法没有返回任何值,但类型void在方法签名中未声明。

查看如何修复“Missing Return Statement”Java软件错误的示例。(@StackOverflow)

10.“Possible Loss of Precision”

当更多的信息被分配给一个变量而超过它的容量,就会发生“Possible Loss of Precision”消息。如果出现这种情况,那么碎片就会被扔掉。 如果这样没有关系,那么代码需要将变量显式声明为新的类型。

50个常见的 Java 错误及避免方法_Java_02

“Possible Loss of Precision”错误通常发生在:

  • 尝试为整数数据类型的变量分配一个实数。

  • 尝试为整数数据类型的变量分配一个double。

此Java中基本数据类型的解释表述了数据特征。(@Oracle)

11.“Reached End of File While Parsing”

当程序缺少关闭大括号(“}”)时,Java代码中就会发生此错误消息。 有时我们可以通过在代码的末尾放置大括号来快速修复错误。

public class mod_MyMod extends BaseModpublic String Version(){     return "1.2_02";
}public void AddRecipes(CraftingManager recipes){
   recipes.addRecipe(new ItemStack(Item.diamond), new Object[] {      "#", Character.valueOf('#'), Block.dirt
   });
}

上述代码将导致以下错误:

java:11: reached end of file while parsing }

编写utilities程序和适当的代码缩进可以更容易地找到这类遗漏的大括号。

此示例表述了缺少了大括号会如何创建“Reached End of File While Parsing”的错误消息。(@StackOverflow)

12.“Unreachable Statement”

当一个语句写在一个阻止它执行的地方时,就会发生“Unreachable statement”错误。通常它出现在中断或返回语句之后。

for(;;){   break;
   ... // unreachable statement}int i=1;if(i==1)
  ...else
  ... // dead code

通常简单地移动返回语句将修复错误。阅读关于如何修复Unreachable Statement Java软件错误的讨论。 (@StackOverflow)

13.“Variable <X> Might Not Have Been Initialized”

当方法中声明的局部变量未初始化时,就会发生这种错误。它也会出现在没有初始值的变量是if语句组成部分的情况下。

int x;if (condition) {
    x = 5;
}
System.out.println(x); // x may not have been initialized

阅读此关于如何避免触发“Variable <X> Might Not Have Been Initialized”错误的讨论。(@reddit)

14.“Operator … Cannot be Applied to <X>”

当操作符用于非定义中的类型时,就会出现此问题。

operator < cannot be applied to java.lang.Object,java.lang.Object

当Java代码尝试在计算中使用类型字符串时,通常会发生这种情况。 要修复的话,就需要将字符串转换为整型或浮点型。

阅读此说明非数字类型如何导致Java软件错误从而警报操作符无法应用于类型的例子。 (@StackOverflow)

15.“Inconvertible Types”

当Java代码尝试执行非法转换时,就会发生“Inconvertible Types”错误。

TypeInvocationConversionTest.java:12: inconvertible types
found   : java.util.ArrayList<java.lang.Class<? extends TypeInvocationConversionTest.Interface1>>
required: java.util.ArrayList<java.lang.Class<?>>
    lessRestrictiveClassList = (ArrayList<Class<?>>) classList;

例如,布尔值不能转换为整型。

阅读此关于如何在Java软件中转换不可转换的类型的讨论。(@StackOverflow)

16.“Missing Return Value”

当return语句包含不正确的类型时,你将收到“Missing Return Value”的消息。例如,以下代码:

public class SavingsAcc2 {    private double balance;    private double interest;    public SavingsAcc2() {
        balance = 0.0;
        interest = 6.17;
    }    public SavingsAcc2(double initBalance, double interested) {
        balance = initBalance;
        interest = interested;
    }    public SavingsAcc2 deposit(double amount) {
        balance = balance + amount;        return;
    }    public SavingsAcc2 withdraw(double amount) {
        balance = balance - amount;        return;
    }    public SavingsAcc2 addInterest(double interest) {
        balance = balance * (interest / 100) + balance;        return;
    }    public double getBalance() {        return balance;
    }
}

返回以下错误:

SavingsAcc2.java:29: missing return value return; 
^ 
SavingsAcc2.java:35: missing return value return; 
^ 
SavingsAcc2.java:41: missing return value return; 
^ 
3 errors

通常,有一个返回语句不返回任何东西。

17.“Cannot Return a Value From Method Whose Result Type Is Void”

当一个void方法尝试返回值时,就会发生此Java错误,例如在以下示例中:

public static void move(){
    System.out.println("What do you want to do?");
    Scanner scan = new Scanner(System.in);    int userMove = scan.nextInt();    return userMove;
}public static void usersMove(String playerName, int gesture){    int userMove = move();    if (userMove == -1)
    {        break;
    }

通常,这可以通过更改方法签名匹配返回语句中的类型来修正错误。在这种情况下,void的实例可以改为int:

public static int move(){
    System.out.println("What do you want to do?");
    Scanner scan = new Scanner(System.in);    int userMove = scan.nextInt();    return userMove;
}

阅读关于如何修复“Cannot Return a Value From Method Whose Result Type Is Void”错误的讨论。(@StackOverflow)

18.“Non-Static Variable … Cannot Be Referenced From a Static Context”

当编译器尝试从静态方法(@javinpaul)访问非静态变量时,就会发生此错误:

public class StaticTest {    private int count=0;    public static void main(String args[]) throws IOException {
        count++; //compiler error: non-static variable count cannot be referenced from a static context
    }
}

要修复“Non-Static Variable … Cannot Be Referenced From a Static Context”错误,可以做这两件事:

  • 在签名中声明此变量为静态。

  • 在静态方法中写代码创建非静态对象的实例。

阅读此介绍静态和非静态变量之间区别的教程。(@sitesbay)

19.“Non-Static Method … Cannot Be Referenced From a Static Context”

此问题发生在Java代码尝试在非静态类中调用非静态方法的情况下。 例如,以下代码:

class Sample{   private int age;   public void setAge(int a)   {
      age=a;
   }   public int getAge()   {      return age;
   }   public static void main(String args[])   {
       System.out.println("Age is:"+ getAge());
   }
}

将返回错误:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
Cannot make a static reference to the non-static method getAge() from the type Sample

从静态方法中调用非静态方法就是声明调用非静态方法的类的实例。

阅读此关于非静态方法和静态方法之间区别的阐述。

20.“(array) <X> Not Initialized”

当数组被声明但未初始化时,你将得到“(array) <X> Not Initialized”的消息。数组的长度是固定的,因此每个数组都需要以所需的长度进行初始化。

以下代码就可以接受:

AClass[] array = {object1, object2}

即:

AClass[] array = new AClass[2];
...array[0] = object1;array[1] = object2;

而非:

AClass[] array;
...array = {object1, object2};

阅读此关于如何在Java软件中初始化数组的讨论。(@StackOverflow)

21.“ArrayIndexOutOfBoundsException”

这是在代码尝试访问不在值内的数组索引时发生的运行时错误消息。以下代码将触发此异常:

String[] name = {    "tom",    "dick",    "harry"};for (int i = 0; i <= name.length; i++) {
    System.out.print(name[i] + '\n');
}

这是另一个例子(@DukeU)

int[] list = new int[5];list[5] = 33; // illegal index, maximum index is 4

数组索引从零开始,结束于小于数组长度的那一个。通常,当定义数组索引的限制时,通过使用“<”而不是“<=”来修复。

查看此关于索引如何触发“ArrayIndexOutOfBoundsException”Java软件错误消息的例子。(@StackOverflow)

22.“StringIndexOutOfBoundsException”

当代码尝试访问不在字符串范围内的字符串的一部分时,就会发生这种问题。通常,这发生在代码尝试创建字符串的子字符串,且长度与参数设置不符之时。下面是一个例子(@javacodegeeks):

public class StringCharAtExample {    public static void main(String[] args) {
        String str = "Java Code Geeks!";
        System.out.println("Length: " + str.length());        //The following statement throws an exception, because
        //the request index is invalid.
        char ch = str.charAt(50);
    }
}

和数组索引一样,字符串索引从零开始。在索引字符串的时候,最后一个字符小于字符串的长度。 “StringIndexOutOfBoundsException”Java软件错误消息通常意味着索引正在尝试访问没有包含的字符。

这里有一个说明“StringIndexOutOfBoundsException”如何发生和修复的例子。(@StackOverflow)

23.“NullPointerException”

当程序尝试使用没有赋值的对象引用时,就会出现“NullPointerException”异常。(@geeksforgeeks)

// A Java program to demonstrate that invoking a method// on null causes NullPointerExceptionimport java.io.*;class GFG{    public static void main (String[] args)    {        // Initializing String variable with null value
        String ptr = null;        // Checking if ptr.equals null or works fine.
        try
        {            // This line of code throws NullPointerException
            // because ptr is null
            if (ptr.equals("gfg"))
                System.out.print("Same");            else
                System.out.print("Not Same");
        }        catch(NullPointerException e)
        {
            System.out.print("NullPointerException Caught");
        }
    }
}

Java程序经常在以下情况下出现异常:

  • 语句引用一个空值的对象。

  • 尝试访问一个已定义但未分配引用的类。

这里有一个开发人员遇到“NullPointerException”以及如何处理它的讨论。(@StackOverflow)

24.“NoClassDefFoundError”

当解释器找不到包含主方法的类的文件时,将发生“NoClassDefFoundError”异常。来自DZone的示例(@DZone):

如果你编译此程序:

class A{  // some code}public class B{    public static void main(String[] args)
    {
        A a = new A();
    }
}

生成两个.class文件:A.class和B.class。删除A.class文件并运行B.class文件,你将得到NoClassDefFoundError的消息:

Exception in thread "main" java.lang.NoClassDefFoundError: A
at MainClass.main(MainClass.java:10)
Caused by: java.lang.ClassNotFoundException: A
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

发生这种情况的原因有:

  • 文件不在正确的目录内。

  • 类的名称必须与文件的名称相同(不包括文件扩展名)。名称分大小写。

阅读此关于运行Java软件时为什么发生“NoClassDefFoundError”的讨论。(@StackOverflow)

25.“NoSuchMethodFoundError”

当Java软件尝试调用类的方法并且该方法不再有定义时,将发生此错误消息(@myUND):

Error: Could not find or load main class wiki.java

当声明中有错字时,通常会出现“NoSuchMethodFoundError”Java软件错误。

阅读此教程以了解如何避免“NoSuchMethodFoundError”的错误消息。(@javacodegeeks)

26.“NoSuchProviderException”

当请求的安全提供程序不可用时,会发生“NoSuchProviderException”异常(@alvinalexander):

javax.mail.NoSuchProviderException

当试图找到为什么发生“NoSuchProviderException”时,请检查:

  • JRE配置。

  • 配置中设置的Java home。

  • 使用哪个Java环境。

  • 安全提供程序条目。

阅读关于在运行Java软件时会导致“NoSuchProviderException”原因的讨论。(@StackOverflow)

27. AccessControlException

AccessControlException表示所请求访问的系统资源,如文件系统或网络是被拒绝的,如本例中的JBossDeveloper(@jbossdeveloper):

ERROR Could not register mbeans java.security.
AccessControlException: WFSM000001: Permission check failed (permission "("javax.management.MBeanPermission" "org.apache.logging.log4j.core.jmx.LoggerContextAdmin#-
[org.apache.logging.log4j2:type=51634f]" "registerMBean")" in code source "(vfs:/C:/wildfly-10.0.0.Final/standalone/deployments/mySampleSecurityApp.war/WEB-INF/lib/log4j-core-2.5.
jar )" of "null")

阅读这篇关于解决方法的讨论,以解决“AccessControlException”错误。(@github)

28.“ArrayStoreException”

当Java数组中转换元素的规则被破坏时,就会发生“ArrayStoreException”异常。对于放到数组中的内容一定要非常小心。(@Roedyg)例如,来自JavaScan.com的这个例子说明此程序(@java_scan):

/* ............... START ............... */
 public class JavaArrayStoreException {     public static void main(String...args) {
         Object[] val = new Integer[4];
         val[0] = 5.8;
     }
 } /* ............... END ............... */

可以产生以下输出:

Exception in thread "main" java.lang.ArrayStoreException: java.lang.Doubleat ExceptionHandling.JavaArrayStoreException.main(JavaArrayStoreException.java:7)

当数组被初始化时,我们需要声明允许进入数组的对象的种类。 每个数组元素都需要成为相同类型的对象。

阅读此关于如何解决“ArrayStoreException”的讨论。(@StackOverflow)

29.“Bad Magic Number”

此Java软件错误消息意味着网络上的类定义文件可能出错了。 以下是来自The Server Side的示例(@TSS_dotcom):

Java(TM) Plug-in: Version 1.3.1_01Using JRE version 1.3.1_01 Java HotSpot(TM) Client VM
User home directory = C:\Documents and Settings\Ankur
Proxy Configuration: Manual ConfigurationProxy: 192.168.11.6:80java.lang.ClassFormatError: SalesCalculatorAppletBeanInfo (Bad magic number)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at sun.applet.AppletClassLoader.findClass(Unknown Source)
at sun.plugin.security.PluginClassLoader.access$201(Unknown Source)
at sun.plugin.security.PluginClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.plugin.security.PluginClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.applet.AppletClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.beans.Introspector.instantiate(Unknown Source)
at java.beans.Introspector.findInformant(Unknown Source)
at java.beans.Introspector.(Unknown Source)
at java.beans.Introspector.getBeanInfo(Unknown Source)
at sun.beans.ole.OleBeanInfo.(Unknown Source)
at sun.beans.ole.StubInformation.getStub(Unknown Source)
at sun.plugin.ocx.TypeLibManager$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at sun.plugin.ocx.TypeLibManager.getTypeLib(Unknown Source)
at sun.plugin.ocx.TypeLibManager.getTypeLib(Unknown Source)
at sun.plugin.ocx.ActiveXAppletViewer.statusNotification(Native Method)
at sun.plugin.ocx.ActiveXAppletViewer.notifyStatus(Unknown Source)
at sun.plugin.ocx.ActiveXAppletViewer.showAppletStatus(Unknown Source)
at sun.applet.AppletPanel.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)

“bad magic number”错误消息可能发生在以下情况下:

  • 类文件的前四个字节不是十六进制数字CAFEBABE。

  • 类文件以ASCII模式而不是以二进制模式上传。

  • Java程序在编译之前运行。

阅读此关于如何找到“bad magic number”异常原因的讨论。(@coderanch)

30.“Broken Pipe”

此错误消息是指来自文件或网络套接字的数据流已停止工作或从另一端关闭(@ExpertsExchange)。

Exception in thread "main" java.net.SocketException: Broken pipe
      at java.net.SocketOutputStream.socketWrite0(Native Method)      at java.net.SocketOutputStream.socketWrite(SocketOutputStream.java:92)
      at java.net.SocketOutputStream.write(SocketOutputStream.java:115)
      at java.io.DataOutputStream.write

出现broken pipe的原因通常有:

  • 耗尽磁盘暂存空间。

  • RAM可能被堵塞。

  • 数据流可能已损坏。

  • 读取管道的过程可能已经关闭。

31.“Could Not Create Java Virtual Machine”

当我们尝试调用带有错误参数的Java代码时,通常会产生此Java错误消息(@ghacksnews):

Error: Could not create the Java Virtual MachineError: A fatal exception has occurred. Program will exit.

这通常是由于代码中的声明存在错误或为其分配适当的内存而引起的。

阅读关于如何修复Java软件错误“Could Not Create Java Virtual Machine”的讨论。(@StackOverflow)

32.“class file contains wrong class”

当Java代码尝试在错误的目录中寻找类文件时,就会出现“class file contains wrong class”的问题,导致类似于以下内容的错误消息:

MyTest.java:10: cannot access MyStruct 
bad class file: D:\Java\test\MyStruct.java file does not contain class MyStruct Please remove or make sure it appears in the correct subdirectory of the classpath. 
MyStruct ms = new MyStruct();

要修复此错误,以下这些提示可以提供帮助:

  • 确保源文件的名称和类的名称匹配——包括大小写。

  • 检查软件包语句是否正确或是否缺失。

  • 确保源文件位于正确的目录中。

阅读此关于如何修复“class file contains wrong class”错误的讨论。(@StackOverflow)

33.“ClassCastException”

“ClassCastException”消息指示了Java代码正在尝试将对象转换为错误的类。在来自Java Concept of Day的这个例子中,运行以下程序:

package com;class A{    int i = 10;
}class B extends A{    int j = 20;
}class C extends B{    int k = 30;
}public class ClassCastExceptionDemo{    public static void main(String[] args)
    {
        A a = new B();   //B type is auto up casted to A type
        B b = (B) a;     //A type is explicitly down casted to B type.
        C c = (C) b;    //Here, you will get class cast exception
        System.out.println(c.k);
    }
}

导致以下错误:

Exception in thread “main” java.lang.ClassCastException: com.B cannot be cast to com.Cat com.ClassCastExceptionDemo.main(ClassCastExceptionDemo.java:23)

Java代码将创建一个类和子类的层次结构。为了避免“ClassCastException”错误,请确保新类型属于正确的类或其父类之一。如果使用泛型,则编译代码时可能会捕获这些错误。

阅读此教程以了解如何修复“ClassCastException”的Java软件错误。(@java_concept)

34.“ClassFormatError”

“ClassFormatError”消息指示链接错误,并且发生在类文件不能被读取或解释为类文件的时候。

Caused by: java.lang.ClassFormatError: Absent Code attribute in method that is        not native or abstract in class file javax/persistence/GenerationTypeat java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(Unknown Source)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)

有若干原因会导致“ClassFormatError”错误:

  • 类文件以ASCII模式而不是二进制模式上传。

  • Web服务器必须以二进制而不是ASCII格式发送类文件。

  • 可能会有一个类路径错误,阻止了代码找到类文件。

  • 如果类被加载两次,那么第二次将导致抛出异常。

  • 正在使用旧版本的Java运行时。

阅读此关于导致Java“ClassFormatError”错误的原因的讨论。(@StackOverflow)

35.“ClassNotFoundException”

“ClassNotFoundException”仅在运行时发生——意味着在编译期间有一个类在运行时缺失了。这是一个链接错误。

50个常见的 Java 错误及避免方法_Java_03

很像“NoClassDefFoundError”,在以下情况下会出现这个问题:

  • 该文件不在正确的目录中。

  • 类的名称必须与文件的名称相同(不包括文件扩展名)。 名称区分大小写。

阅读此关于导致“ClassNotFoundException”原因的更多案例的讨论。(@StackOverflow)。

36.“ExceptionInInitializerError”

此Java问题发生在静态初始化出错的时候(@GitHub)。 当Java代码稍后使用该类时,将发生“NoClassDefFoundError”错误。

java.lang.ExceptionInInitializerError
  at org.eclipse.mat.hprof.HprofIndexBuilder.fill(HprofIndexBuilder.java:54)
  at org.eclipse.mat.parser.internal.SnapshotFactory.parse(SnapshotFactory.java:193)
  at org.eclipse.mat.parser.internal.SnapshotFactory.openSnapshot(SnapshotFactory.java:106)
  at com.squareup.leakcanary.HeapAnalyzer.openSnapshot(HeapAnalyzer.java:134)
  at com.squareup.leakcanary.HeapAnalyzer.checkForLeak(HeapAnalyzer.java:87)
  at com.squareup.leakcanary.internal.HeapAnalyzerService.onHandleIntent(HeapAnalyzerService.java:56)
  at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:145)  at android.os.HandlerThread.run(HandlerThread.java:61)
Caused by: java.lang.NullPointerException: in == null
  at java.util.Properties.load(Properties.java:246)  at org.eclipse.mat.util.MessageUtil.(MessageUtil.java:28)  at org.eclipse.mat.util.MessageUtil.(MessageUtil.java:13)
  ... 10 more

修复此错误我们需要更多的信息。在代码中使用getCause()可以返回导致错误的异常。

阅读此关于如何追踪ExceptionInInitializerError原因的讨论。(@StackOverflow)

37.“IllegalBlockSizeException”

当长度消息不是8字节的倍数时,那么在解密期间就会抛出“IllegalBlockSizeException”异常。以下是一个出自ProgramCreek.com的示例(@ProgramCreek):

@Overrideprotected byte[] engineWrap(Key key) throws IllegalBlockSizeException, InvalidKeyException {    try {        byte[] encoded = key.getEncoded();        return engineDoFinal(encoded, 0, encoded.length);
    } catch (BadPaddingException e) {
        IllegalBlockSizeException newE = new IllegalBlockSizeException();
        newE.initCause(e);        throw newE;
    }
}

“IllegalBlockSizeException”可能是由以下原因引起的:

  • 使用不同的加密和解密算法选项。

  • 要解密的消息可能在传输中被截断或乱码。

阅读关于如何防止IllegalBlockSizeException Java软件错误消息的讨论。(@StackOverflow)

38.“BadPaddingException”

当使用填充来创建一个消息而不是8字节的倍数时,那么在解密期间可能会出现“BadPaddingException”异常。这是出自Stack Overflow的一个例子(@StackOverflow):

javax.crypto.BadPaddingException: Given final block not properly paddedat com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)at com.sun.crypto.provider.SunJCE_f.b(DashoA13*..)at com.sun.crypto.provider.AESCipher.engineDoFinal(DashoA13*..)at javax.crypto.Cipher.doFinal(DashoA13*..)

加密数据是二进制的,所以不要尝试将其存储在字符串或在加密期间没有被正确填充的数据中。

阅读关于如何防止BadPaddingException的讨论。(@StackOverflow)

39.“IncompatibleClassChangeError”

“IncompatibleClassChangeError”是LinkageError的一种形式,如果一个在基类在编译子类之后发生变化,那么就会出现此异常。下面这个例子来自于How to Do in Java(@HowToDoInJava):

Exception in thread "main" java.lang.IncompatibleClassChangeError: Implementing classat java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
at net.sf.cglib.core.DebuggingClassWriter.toByteArray(DebuggingClassWriter.java:73)
at net.sf.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:26)
at net.sf.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:216)
at net.sf.cglib.core.KeyFactory$Generator.create(KeyFactory.java:144)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:116)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:108)
at net.sf.cglib.core.KeyFactory.create(KeyFactory.java:104)
at net.sf.cglib.proxy.Enhancer.(Enhancer.java:69)

出现“IncompatibleClassChangeError”有可能的原因是:

  • 忘记了主方法的静态。

  • 非法使用了legal类。

  • 类被改变了,并且存在通过旧的签名从另一个类到这个类的引用。尝试删除所有类文件并重新编译所有内容。

尝试解决“IncompatibleClassChangeError”的这些步骤(@javacodegeeks)

40.“FileNotFoundException”

当具有指定路径名的文件不存在时,将抛出此Java软件错误消息。

@Override public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {    if (uri.toString().startsWith(FILE_PROVIDER_PREFIX)) {        int m = ParcelFileDescriptor.MODE_READ_ONLY;        if (mode.equalsIgnoreCase("rw")) m = ParcelFileDescriptor.MODE_READ_WRITE;
        File f = new File(uri.getPath());
        ParcelFileDescriptor pfd = ParcelFileDescriptor.open(f, m);        return pfd;
    } else {        throw new FileNotFoundException("Unsupported uri: " + uri.toString());
    }
}

除了没有指定路径名的文件之外,这可能意味着现有文件无法访问。

阅读关于为什么会抛出“FileNotFoundException”的讨论。(@StackOverflow)

41.“EOFException”

当输入期间意外终止文件或流时,将抛出“EOFException”。 以下是抛出EOFException异常的一个示例,来自JavaBeat应用程序:

import java.io.DataInputStream;import java.io.EOFException;import java.io.File;import java.io.FileInputStream;import java.io.IOException;public class ExceptionExample {    public void testMethod1() {
        File file = new File("test.txt");
        DataInputStream dataInputStream = null;        try {
            dataInputStream = new DataInputStream(new FileInputStream(file));            while (true) {
                dataInputStream.readInt();
            }
        } catch (EOFException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {            try {                if (dataInputStream != null) {
                    dataInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }    public static void main(String[] args) {
        ExceptionExample instance1 = new ExceptionExample();
        instance1.testMethod1();
    }
}

运行上面的程序,将抛出以下异常:

java.io.EOFExceptionat java.io.DataInputStream.readInt(DataInputStream.java:392)at logging.simple.ExceptionExample.testMethod1(ExceptionExample.java:16)at logging.simple.ExceptionExample.main(ExceptionExample.java:36)

当DataInputStream类尝试在流中读取数据但没有更多数据时,将抛出“EOFException”。它也可以发生在ObjectInputStream和RandomAccessFile类中。

阅读关于运行Java软件时可能发生“EOFException”的讨论。(@StackOverflow)

42.“UnsupportedEncodingException”

当不支持字符编码时,会抛出此Java软件错误消息(@Penn)。

public UnsupportedEncodingException()

正在使用的Java虚拟机可能不支持给定的字符集。

阅读关于如何在运行Java软件时处理“UnsupportedEncodingException”异常的讨论。(@StackOverflow)

43.“SocketException”

“SocketException”异常表示创建或访问套接字时出错(@ProgramCreek)。

public void init(String contextName, ContextFactory factory) {    super.init(contextName, factory);
    String periodStr = getAttribute(PERIOD_PROPERTY);    if (periodStr != null) {        int period = 0;        try {
            period = Integer.parseInt(periodStr);
        } catch (NumberFormatException nfe) {}        if (period <= 0) {            throw new MetricsException("Invalid period: " + periodStr);
        }
        setPeriod(period);
    }
    metricsServers =
        Util.parse(getAttribute(SERVERS_PROPERTY), DEFAULT_PORT);
    unitsTable = getAttributeTable(UNITS_PROPERTY);
    slopeTable = getAttributeTable(SLOPE_PROPERTY);
    tmaxTable = getAttributeTable(TMAX_PROPERTY);
    dmaxTable = getAttributeTable(DMAX_PROPERTY);    try {
        datagramSocket = new DatagramSocket();
    } catch (SocketException se) {
        se.printStackTrace();
    }
}

当由于以下原因而达到最大连接时,通常会抛出此异常:

  • 没有更多的网络端口可用于应用程序。

  • 系统没有足够的内存来支持新的连接。

阅读关于如何在运行Java软件时解决“SocketException”问题的讨论。(@StackOverflow)

44.“SSLException”

此Java软件错误消息发生在与SSL相关的操作出现故障的时候。 以下示例来自Atlassian(@Atlassian):

com.sun.jersey.api.client.ClientHandlerException: javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
   at com.sun.jersey.client.apache.ApacheHttpClientHandler.handle(ApacheHttpClientHandler.java:202)
   at com.sun.jersey.api.client.Client.handle(Client.java:365)
   at com.sun.jersey.api.client.WebResource.handle(WebResource.java:556)
   at com.sun.jersey.api.client.WebResource.get(WebResource.java:178)
   at com.atlassian.plugins.client.service.product.ProductServiceClientImpl.getProductVersionsAfterVersion(ProductServiceClientImpl.java:82)
   at com.atlassian.upm.pac.PacClientImpl.getProductUpgrades(PacClientImpl.java:111)
   at com.atlassian.upm.rest.resources.ProductUpgradesResource.get(ProductUpgradesResource.java:39)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
   at java.lang.reflect.Method.invoke(Unknown Source)
   at com.atlassian.plugins.rest.common.interceptor.impl.DispatchProviderHelper$ResponseOutInvoker$1.invoke(DispatchProviderHelper.java:206)
   at com.atlassian.plugins.rest.common.interceptor.impl.DispatchProviderHelper$1.intercept(DispatchProviderHelper.java:90)
   at com.atlassian.plugins.rest.common.interceptor.impl.DefaultMethodInvocation.invoke(DefaultMethodInvocation.java:61)
   at com.atlassian.plugins.rest.common.expand.interceptor.ExpandInterceptor.intercept(ExpandInterceptor.java:38)
   at com.atlassian.plugins.rest.common.interceptor.impl.DefaultMethodInvocation.invoke(DefaultMethodInvocation.java:61)
   at com.atlassian.plugins.rest.common.interceptor.impl.DispatchProviderHelper.invokeMethodWithInterceptors(DispatchProviderHelper.java:98)
   at com.atlassian.plugins.rest.common.interceptor.impl.DispatchProviderHelper.access$100(DispatchProviderHelper.java:28)
   at com.atlassian.plugins.rest.common.interceptor.impl.DispatchProviderHelper$ResponseOutInvoker._dispatch(DispatchProviderHelper.java:202)
   ...Caused by: javax.net.ssl.SSLException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
   ...Caused by: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty
   ...Caused by: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty

发生这种情况的原因有:

  • 服务器或客户端上的证书已过期。

  • 服务器端口已重置为另一个端口。

阅读关于可能导致Java软件“SSLException”错误的讨论。(@StackOverflow)

45.“MissingResourceException”

当资源丢失时,会发生“MissingResourceException”异常。如果资源在正确的类路径中,那么通常是因为属性文件没有正确配置。下面有一个例子(@TIBCO):

java.util.MissingResourceException: Can't find bundle for base name localemsgs_en_US, locale en_US
java.util.ResourceBundle.throwMissingResourceException
java.util.ResourceBundle.getBundleImpl
java.util.ResourceBundle.getBundle
net.sf.jasperreports.engine.util.JRResourcesUtil.loadResourceBundle
net.sf.jasperreports.engine.util.JRResourcesUtil.loadResourceBundle

阅读关于如何在运行Java软件时修复“MissingResourceException”的讨论。

46.“NoInitialContextException”

当Java应用程序想要执行命名操作但无法创建连接时,会发生“NoInitialContextException”异常(@TheASF)。

[java] Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file:  java.naming.factory.initial[java]     at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:645)[java]     at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:247)[java]     at javax.naming.InitialContext.getURLOrDefaultInitCtx(InitialContext.java:284)[java]     at javax.naming.InitialContext.lookup(InitialContext.java:351)[java]     at org.apache.camel.impl.JndiRegistry.lookup(JndiRegistry.java:51)

这解决起来可能会是一个复杂的问题,但这里有一些可能导致“NoInitialContextException”Java错误消息的原因:

  • 应用程序可能没有正确的凭据进行连接。

  • 代码可能无法识别所需的JNDI实现。

  • InitialContext类可能没有配置正确的属性。

阅读关于运行Java软件时“NoInitialContextException”意味什么的讨论。(@StackOverflow)

47.“NoSuchElementException”

当迭代(例如“for”循环)尝试访问下一个元素而没有元素的时候,就会出现“NoSuchElementException”异常。

public class NoSuchElementExceptionDemo{    public static void main(String args[]) {
        Hashtable sampleMap = new Hashtable();
        Enumeration enumeration = sampleMap.elements();
        enumeration.nextElement();  //java.util.NoSuchElementExcepiton here because enumeration is empty
    }
}
Output:
Exception in thread "main" java.util.NoSuchElementException: Hashtable Enumerator
        at java.util.Hashtable$EmptyEnumerator.nextElement(Hashtable.java:1084)
        at test.ExceptionTest.main(NoSuchElementExceptionDemo.java:23)

抛出“NoSuchElementException”可能的途径:

  • Enumeration:: nextElement()

  • NamingEnumeration::next()

  • StringTokenizer:: nextElement()

  • Iterator::next()

阅读此关于如何在Java软件中修复“NoSuchElementException”的教程。(@javinpaul)

48.“NoSuchFieldError”

当应用程序尝试访问对象中的一个字段,但指定的字段不再存在于对象中时,将抛出此Java软件错误消息(@sourceforge)。

public NoSuchFieldError()

通常,该错误在编译器中被捕获,但是如果在编译和运行之间更改了类定义,则在运行时将被捕获。

阅读此关于如何在运行Java软件时发现什么导致“NoSuchFieldError”的讨论。(@StackOverflow)

49.“NumberFormatException”

当应用程序尝试将字符串转换为数字类型,但该数字不是有效的数字字符串时,会出现此Java软件错误消息(@alvinalexander)。

package com.devdaily.javasamples;public class ConvertStringToNumber {    public static void main(String[] args) {        try {
            String s = "FOOBAR";            int i = Integer.parseInt(s);            // this line of code will never be reached
            System.out.println("int value = " + i);
        }        catch (NumberFormatException nfe) {
            nfe.printStackTrace();
        }
    }
}

可能抛出“NumberFormatException”的原因有:

  • 数字中的前导或尾随空格。

  • 标志不在数字前面。

  • 数字有逗号。

  • 本地化可能不会将其分类为有效数字。

  • 数字太大,不适合数字类型。

阅读关于如何在运行Java软件时避免“NumberFormatException”的讨论。(@StackOverflow)。

50.“TimeoutException”

当阻塞操作超时时,会出现此Java软件错误消息。

private void queueObject(ComplexDataObject obj) throws TimeoutException, InterruptedException {    if (!queue.offer(obj, 10, TimeUnit.SECONDS)) {
        TimeoutException ex = new TimeoutException("Timed out waiting for parsed elements to be processed. Aborting.");        throw ex;
    }
}

阅读此关于如何在运行Java软件时处理“TimeoutException”的讨论。(@StackOverflow)。

结论

到这里就全部完结了! 如果你细细阅读了整篇文章,那么相信现在你应该能处理各种运行时和编译器的错误和异常了。编程愉快!


翻译作者:码农网 – 小峰

 

50个常见的 Java 错误及避免方法_Java_04