Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Why am I getting StackOverflow errors?

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why am I getting StackOverflow errors?

    This is the letter-searching part of a Java Boggle program. Boggle is a game where you have a 5x5 board (in my case) and you try to find words by joining adjacent letters on the board.
    For my assignment, I had to use recursion rather than an iterative approach. Here goes:

    boolean findStart (String word, int x, int y){
    found = false;
    String firstLetter;
    firstLetter=word.substring(0,1);
    if (firstLetter.equals(letters[x][y])){
    found = findWord(word, x, y, new boolean[5][5]);
    }
    else {
    y++;
    if (y > 4){
    y = 0;
    x++;
    }
    if (x > 4)
    return false;

    }
    return findStart(word,x,y);

    }
    boolean findWord (String word, int x, int y, boolean[][] visited){

    if (word.length() == 1){
    found = true;
    }
    else
    {
    visited[x][y] = true;
    List<Point> adjoining = getAdjoining(x,y,visited);

    for (Point p : adjoining){
    found = findWord(word.substring(1), p.x, p.y, visited);
    if (found)
    return true;
    }
    visited[x][y] = false;
    }

    return false;
    }


    Here are the errors:
    Exception in thread "AWT-EventQueue-1" java.lang.StackOverflowError
    at Assignment1B.getAdjoining(Assignment1B.java:117)
    at Assignment1B.findWord(Assignment1B.java:103)
    at Assignment1B.findWord(Assignment1B.java:106)
    at Assignment1B.findStart(Assignment1B.java:80)
    at Assignment1B.findStart(Assignment1B.java:92)

    I didn't post my getAdjoining method, but it was just a method to find the adjoining letters on the board.
    Where did I go wrong?
    Thanks so much!


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,273
    Thanks
    177
    Thanked 822 Times in 765 Posts
    Blog Entries
    5

    Default Re: Why am I getting StackOverflow errors?

    StackOverflowError occur when you pile up method calls in the stack - in other words, the recursion goes so deep it overflows the java stack. You can increase the stack size using the -Xss <size> command line parameter, but I would say you might be better off trying to fix the underlying problem.

Similar Threads

  1. help with errors
    By hello_world in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 9th, 2011, 09:11 PM
  2. Can't fix my own errors
    By mrroberts2u in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 14th, 2011, 09:20 AM
  3. Many Errors
    By Woody619 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: July 16th, 2010, 09:36 PM
  4. Getting errors
    By Nonire in forum What's Wrong With My Code?
    Replies: 7
    Last Post: July 4th, 2010, 12:21 PM
  5. Why am I getting 62 errors?
    By DestinyChick1225 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: July 1st, 2010, 04:41 AM