2017-03-05 35 views
-2
public static void main(String[] args) { 

    CashRegister myCashRegister = new CashRegister(); 

    Item item = myCashRegister.getItem(123456); 



public class CashRegister { 
    public Item getItem(int barcode) { 
     return this.getItem(barcode); 
    } 



public class Item { 

    public int getBarcode(){ 
     return barcode; 
    } 

我在main中創建一個item對象,試圖通過Cash Register類獲取該條目的條形碼。我最終得到「線程中的異常」主「java.lang.StackOverflowError」。獲取條形碼 - 線程「main」中的異常java.lang.StackOverflowError

我在項目類中有一個getBarcode方法,但不斷收到錯誤,因爲條碼是一個整數,所以我不能返回一個int爲Item getItem方法。

請讓我在正確的方向獲得項目的條形碼。 謝謝。

回答

0

你已經編程了一個無限循環。

public Item getItem(int barcode) { 
    return this.getItem(barcode); 
} 

這裏你一次又一次地調用同樣的方法...

+0

哈!好吧,我現在看到它。我正在調用與我正在使用的方法相同的方法。對不起,浪費你的時間去尋找那麼明顯的東西。我是一名初學者,需要學習很多東西。我只是不知道如何爲這個方法返回一個int並嘗試隨機的東西。 – jr99

+0

沒問題。 如果答案對您有幫助,您能否請您將我的答案標記爲正確。謝謝。 – Markus

相關問題