2013-10-13 40 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的最新更新。

相關問題