-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;
    }

I am creating an item object in main and trying to get the item's barcode through a Cash Register class. I end up getting "Exception in thread "main" java.lang.StackOverflowError".

I do have a getBarcode method in the item class, but kept getting errors because barcode is an int, and so I cannot return an int for the Item getItem method.

Please put me in the right direction towards getting the item's barcode. Thank you.

jr99
  • 1
  • 2

1 Answers1

0

You have programmed an endless loop.

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

Here you are calling the same method again and again...

Markus
  • 1,141
  • 1
  • 9
  • 25
  • Ha! Okay I see it now. I am calling the same method that I am using. Sorry for wasting your time for something so obvious. I am a beginner and have a lot to learn. I just do not know how to return an int for this method and tried random stuff. – jr99 Mar 05 '17 at 18:53
  • No problem. If the answer has helped you, can you please mark my answer as correct. Thank you. – Markus Mar 05 '17 at 18:59