Java error: "Exception in thread "main" java.lang.StackOverflowError" issue in constructor

StackOverflow https://stackoverflow.com/questions/22202815

  •  08-06-2023
  •  | 
  •  

Вопрос

I'm really really stuck and hope that someone can help me. I read and read and still don't understand how to fix my stackoverflow error. I know that its in my constructor, but i don't know how to fix it.

I am creating a derived class called FractionBottle that extends the Bottle class. The FractionBottle class as a private data memeber: Fraction myFraction = new Fraction(); Here is my constructor in my Bottle Class:

 public class Bottle


    private final int MAX_PILLS = 120;
    private int pillsInBottle;

public Bottle()
{
    pillsInBottle = 0;


}

Here's what I have in my FractionBottle class:

public class FractionBottle extends Bottle
{
    Fraction myFraction = new Fraction();


    public FractionBottle()
    {
        super();
        myFraction.getNumerator();
        myFraction.getDenominator();
    }




    public FractionBottle(int wholeValue, int num, int den)
    {
        super(wholeValue);
        myFraction.set(num, den);;
    }

    public void read()
    {
        super.read();
        System.out.println("Pleas enter value for fraction part:");
        myFraction.read();
    }

    public FractionBottle add(FractionBottle other)
    {
        FractionBottle sumOfBottles = new FractionBottle();

        sumOfBottles = this.add(other);
        sumOfBottles.myFraction = this.myFraction.add(other.myFraction);


        return (sumOfBottles);
    }

Here's the demo I'm using:

public class FractionBottleDemo 
{
    public static void main (String args[])
    {


        FractionBottle fbl1 = new FractionBottle();
        FractionBottle fbl2 = new FractionBottle();
        FractionBottle fbl3 = new FractionBottle();

        System.out.println("Enter info for whole value for fbl1: ");
        fbl1.read();
        System.out.println("Enter infor for whole value for fbl2: ");
        fbl2.read();
        System.out.println(fbl1);
        System.out.println(fbl2);


         fbl3 = fbl1.add(fbl2);



    }

}

I am really stuck on this assignment for class, and I've been at it for a few days now. I'm getting the following error:

  Exception in thread "main" java.lang.StackOverflowError
    at FractionBottle.<init>(FractionBottle.java:7)
    at FractionBottle.add(FractionBottle.java:32)

the last lin repeats several times...

Please tell me how to fix this! I know its going into a infinite recursive loop. but i don't know how to fix it. My add method in my FractionBottle class, must return a FractionBottle.

Thank you!!!!

Это было полезно?

Решение

Looks like you have an infinite recursive call inside the add method.

sumOfBottles = this.add(other);

All recursive functions require a check to break out of the recursive calls.

Since, you are wanting to call the Bottle's add method.

Replace above line with

sumOfBottles = super.add(other);

Другие советы

Obviously the error lies here:

public FractionBottle add(FractionBottle other){
    ...
    sumOfBottles = this.add(other);
    ...
}

because you are calling to the add method from this very method, and that causes an infinite number of recursive callings and then, your StackOverflowError

Maybe what you want to do is to call Bottle's add method that is:

public FractionBottle add(FractionBottle other){
        ...
        Bottle sumOfBottles = new FractionBottle();
        sumOfBottles = super.add(other);
        ...
    }

that is much safer and wouldn't cause infinite loops

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top