Results 1 to 4 of 4
  1. #1
    ausglanville is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default Need Help - StackOverflowError - Fibonacci

    I'm creating a Fibonacci sequence for a program. The instructions are as follows: "Write a recursive method to compute a fibonocci series. Start at a user supplied starting point and continue 10 places."

    Java Code:
    import javax.swing.JOptionPane;
    
    public class Fibonacci
    {
     static String series = "";
     static String input = "";
     public static void main(String[] args)
     {
      input = JOptionPane.showInputDialog(null, "Enter an integer.");
      int start = Integer.parseInt(input);
      fib(start, 0);
      JOptionPane.showMessageDialog(null, "The series is \n" + series);
     }
     /** Computes a fibonocci series starting at a user supplied integer and continuing 10 places. */
     public static String fib(int x, int y)
     {
      int i = 0;
      int result = 0;
      if(i < 2)
      {
       result = x;
       y = x;
      }
      else
      {
       result = x + y;
       y = x;
       x = result;
      }
     [B] series += ", " + result;[/B] 
      i++;  
      if(i < 10)
        fib(x,y);
      return series;
     }
    }
    When compiled, the method shows no errors. However, when tested, the bolded line returns the following error: "java.lang.StackOverflowError: null(in java.lang.Stringbuilder)"

    As you can tell, I'm quite new to java. I don't know if I'm just not understanding the problem, or if I am, but just going about it the wrong way. Any help is appreciated.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    It may help to forget about producing the series for now and just produce the sum of the fibbonaci series as an integer result. Once you do that it may be easier to build the string representing the series.

    Here are some things to think about when working on recursion.

    1) you should have a termination condition, if this condition is met it should produce the final answer.
    2) if the termination condition is not met it should preform a recursive call.

    How well do you understand recursion?

    Java Code:
    public static int fact(int n){
      if(n == 1){ //termination condition
        return 1;
      }
      else{ //termination condition not met
        return n * fact(n - 1);
      }
    }
    This is a recursive call to find the factorial of some number, perhaps it will help you.
    Last edited by sunde887; 04-13-2011 at 02:59 AM.

  4. #4
    ausglanville is offline Member
    Join Date
    Apr 2011
    Posts
    2
    Rep Power
    0

    Default

    I found a way to fix it, although I don't quite know if it is the most efficient way. When I declared i and result, both were within the fib method. So each time the fib method is called, i and result are reset, therefore i never meets the i >= 10 exit condition and the loop runs until it runs out of memory. By declaring them as static variables outside of all methods, the problem is fixed.

Similar Threads

  1. StackOverflowError
    By selva.bics in forum AWT / Swing
    Replies: 5
    Last Post: 08-05-2011, 01:09 PM
  2. java.lang.StackOverflowError
    By malstryx in forum New To Java
    Replies: 9
    Last Post: 10-01-2008, 04:14 AM
  3. java.lang.StackoverflowError
    By ravisankarvivek in forum New To Java
    Replies: 6
    Last Post: 06-23-2008, 09:05 AM
  4. java.lang.StackOverFlowError exception
    By jayaj in forum NetBeans
    Replies: 1
    Last Post: 06-08-2008, 11:17 AM
  5. java.lang.StackOverflowError
    By eva in forum New To Java
    Replies: 3
    Last Post: 12-24-2007, 09:54 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
  •