Results 1 to 13 of 13
  1. #1
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default StackOverFlow Error

    Hi,

    I am getting a "StackOverFlow" error with the following code:-
    Java Code:
    //This Application displays fibonacci sequence upto the n numbers
    
    class Fibonacci
    {
    	  int upto;
    	 private int result;
    /*Constructor for initializing the the max number upto which the
    *  the fibonacci sequence will be displayed
    */
    	Fibonacci(int len)
    	{
    		upto = len;
    	}
    
    //Routine to display the fibonacci sequence
    	int calFibonacci(int i)
    	{
    		if(i > upto)
    			{
    			return 0;
    			}
    		if(i == 0)
    		{
    			result=0;
    		}
    		if(i == 1)
    		{
    			result=1;
    		}
    		else
    			//calFibonacci(i) = calFibonacci(i-1) + calFibonacci(i-2);
    			result = calFibonacci(i-1) + calFibonacci(i-2);
    		return result;
    	}
    	
    }
    
    
    
    class dispFibonacci {
    	public static void main(String[] args)
    	{
    		Fibonacci ob1 =new Fibonacci(10);
    		System.out.println("Displaying the fibonacci sequence upto::"+ ob1.upto);
    		int fElement;
    		for(int iMin = 0; iMin < ob1.upto; iMin++)
    		{
    			fElement = ob1.calFibonacci(iMin);
    			System.out.println(fElement);	
    		}	
    	}
    
    }
    The error is as follows:-
    StackOverFlow Error-fibonacci.png

    Seeking for help, thanks in advance.

    Kind regards,
    Ankit

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: StackOverFlow Error

    StackOverflow errors occur when you have a method that calls itself, and then calls itself etc without ever stopping.

    What value is calFibinacci() being called with when you get into this endless loop? You can find out by using System.out.println().

    Java Code:
    //Routine to display the fibonacci sequence
    int calFibonacci(int i)
    {
        System.out.println("calFibonacci called with i=" + i);
        if(i > upto)
        {
            return 0;
        }
        // etc

  3. #3
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: StackOverFlow Error

    Hi pbrockway2,

    Thanks for the information.

    The first value that is being passed is 0, but after 0 all the values that are being passed are negative and keeps piling on.

    I am not sure about why this is happening since I am incrementing the value of the variable "i" that I am passing to the function.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: StackOverFlow Error

    I am not sure about why this is happening since I am incrementing the value of the variable "i" that I am passing to the function.
    Yes, but in the method you are calling the method again with smaller values:

    Java Code:
    else
        //calFibonacci(i) = calFibonacci(i-1) + calFibonacci(i-2);
        result = calFibonacci(i-1) + calFibonacci(i-2);
    // <-- the method will never get here
    return result;
    Go slowly through the method as you have written it to see why it will never get to the return statement if you pass it the value 0.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: StackOverFlow Error

    Also posted at javaprogrammingforums.com

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default Re: StackOverFlow Error

    Carefully check what parts of the code in the calFibonacci method is executed when the parameter is, say, 0. You want an if - else if - else if -else construct, not an if - if - else construct.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: StackOverFlow Error

    Hi,

    I am still not able to figure out the problem.

    need some more time.

    Apologies for the inconvenience caused.

    Kind Regards.
    Ankit

  8. #8
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: StackOverFlow Error

    hi pbrockway2,


    But i want to display the Fibonacci sequence, so i thought the method would work out this way.

    It will take some more time for me to figure the problem.

    Thanks in advance

    kind regards.
    ankit

  9. #9
    nandha is offline Member
    Join Date
    Jan 2012
    Posts
    2
    Rep Power
    0

    Default Re: StackOverFlow Error

    the problem is using to much of if statement in the method calFibonacci().

    u can use else if statement

    int calFibonacci(int i)
    {
    if(i > upto)
    {
    return 0;
    }
    else if(i == 0)
    {
    result=0;
    }
    else if(i == 1)
    {
    result=1;
    }
    else
    {
    result = calFibonacci(i-1) + calFibonacci(i-2);
    }
    return result;
    }

  10. #10
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: StackOverFlow Error

    Hi Jos,

    Thanks a lot your suggestion solved my problem. :)

    But I am still not sure why :( .

    I'll have to go through if else tutorial.

    Thanks for your valuable suggestions. :)

    kind regards,
    Ankit

  11. #11
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: StackOverFlow Error

    Quote Originally Posted by nandha View Post
    the problem is using to much of if statement in the method calFibonacci().

    u can use else if statement

    int calFibonacci(int i)
    {
    if(i > upto)
    {
    return 0;
    }
    else if(i == 0)
    {
    result=0;
    }
    else if(i == 1)
    {
    result=1;
    }
    else
    {
    result = calFibonacci(i-1) + calFibonacci(i-2);
    }
    return result;
    }
    All that your comment and suggestion prove is your lack of understanding of the return keyword.

    Oh, and when posting on a technical forum, please Write in clear, grammatical, correctly-spelled language.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  12. #12
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: StackOverFlow Error

    Hi db,

    yes my understanding regarding the return is not much as of now, i've just read it once.

    Thanks for your suggestions, they will be taken care in next post. :)

    kind regards,
    Ankit

  13. #13
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

Similar Threads

  1. StackOverFlow
    By s0meb0dy in forum New To Java
    Replies: 1
    Last Post: 02-17-2011, 06:50 AM
  2. StackOverflow Exception with Regexes
    By masterrs.mind in forum Advanced Java
    Replies: 6
    Last Post: 03-12-2010, 04:32 PM
  3. Replies: 1
    Last Post: 03-10-2010, 01:53 PM
  4. Replies: 1
    Last Post: 12-05-2009, 07:28 AM
  5. Replies: 3
    Last Post: 01-23-2009, 08:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •