2013-10-13 39 views
-1

当我运行我的代码不知道什么是错在这里我得到这个错误:Java编译错误:异常线程“main” java.lang.VerifyError的:

Exception in thread "main" java.lang.VerifyError: (class: first3weeks/Main, method: <init> signature:()V) Constructor must call super() or this() 
Java Result: 1 

学生守则:

package first3weeks; 

public class Student { 
    private String name, id; 
    private int[] score = new int[3]; 


    public Student(){} 

    public Student(String stName, String stID, int stScore[]){ 
     name = stName; 
     id = stID; 
     score = stScore; 
    } 

    public void setName(String nameIn){ 
     name = nameIn; 
    } 

    public void setID(String idIn){ 
     id = idIn; 
    } 

    public void setScore(int scoreIn[]){ 
     score = scoreIn; 
    } 

    public String getName(){ 
     return name; 
    } 

    public String getID(){ 
     return id; 
    } 

    public int[] getScore(){ 
     return score; 
    } 

    public double avScore(){ 
     double total = score[1] + score[2] + score[3]; 
     return (total/3); 
    } 

    public void printOut(){ 
     System.out.println("Student Name: " + getName() + "\n" + "Student ID: " + getID() + "\n" + "Student Average: " + avScore()); 
    } 
} 

主类:

package first3weeks; 

public class Main { 

    public static void main(String[] args) { 
     int[] score1 = {12,15,19}; 
     int[] score2 = {32,65,29}; 
     Student stud1 = new Student("Rob", "001", score1); 
     Student stud2 = new Student("Jeff", "002", score2); 
     stud1.printOut(); 
     stud2.printOut(); 

     Student stud3 = new Student(); 
     int[] score3 = {56,18,3}; 
     stud3.setName("Richard"); 
     stud3.setID("003"); 
     stud3.setScore(score3); 
     stud3.printOut(); 
    } 
} 
+0

错误在程序包的前3周 – hrv

+1

当您的类文件中存在不一致时,会发生java.lang.VerifyError。清理你的项目。编译并重新运行! –

+0

我发现这个问题在主类中,我留下了一些代码之间的换行符,并在删除它之后,修复了错误,现在它的运行正常, 感谢贡献:) – rob1994

回答

1

我跑用java的1.7.0_17版本,我得到是唯一的例外代码。一个java.lang.ArrayIndexOutOfBoundsException: 3 **

在java中,数组是从零开始的索引,即第一个元素具有零指数,所以在方法avScore你应该做的:

public double avScore(){ 
     double total = score[0] + score[1] + score[2]; 
     return (total/3); 
} 
+1

虽然这是一个错误,它与OP得到的错误没有任何关系。此外,该功能永远不会被调用。 –

+0

@AniketThakur''avScore'方法由'printOut'调用。 – mabbas

+0

没有看到它。对此抱歉,但OP所面临的问题是不同的,主要是由于类文件不一致。尽管他还需要解决你指出的问题。 –

2

此错误

Exception in thread "main" java.lang.VerifyError: (class: first3weeks/Main, 
    method: <init> signature:()V) Constructor must call super() or this() 

表示字节码尚未正确生成。这可能是编译器中的一个错误。我将确保您拥有Java 7更新40或Java 6更新45的最新更新。

相关问题