0

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!!!!

8
  • 2
    What does your add method do? Describe it in words. Commented Mar 5, 2014 at 16:02
  • Your code does not match the call stack. Please post the full code Commented Mar 5, 2014 at 16:03
  • So, the bottle class has a int value (pillsInBottle), the add method is suppose to add the pillsInBottle, and add the myFraction portion of each FractionBottle. It then is suppose to return a FractionBottle. Commented Mar 5, 2014 at 16:04
  • Looks like you have an infinite loop inside the add method Commented Mar 5, 2014 at 16:04
  • In your add(FractionBottle)-method you are doing a recursive call to add(FractionBottle) with the same argument. This looks dangerous... Commented Mar 5, 2014 at 16:04

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

Comments

0

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

10 Comments

oh, that makes sense!i guess What i meant to do was call the bottle add method. How can I do that? Here is my full Bottle class:
public Bottle add(int amount) { Bottle b = new Bottle(); b.pillsInBottle = this.pillsInBottle + amount; if ((b.pillsInBottle > MAX_PILLS) || (b.pillsInBottle < 0)) { System.out.println("This amount cannot be added to this bottle."); System.exit(0); } return (b); } @user3384292
@user3384292 You're Bottle class doesn't have an add method
@user3384292 If it does have an add method just say super.add(parameters)
@NappaTheSaiyan i guess not in the snippet, but it should be implemented in the super class
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.