StackOverFlow error

my program compiles, but outputs a stackoverflow error. I dont know where the error is and how to fix it though. =[

This is my code any help would be greatly appreciated:

package cop3530;

import java.io.FileReader;

import java.io.IOException;

import java.util.List;

import java.util.ArrayList;

import java.util.Scanner;

import java.util.StringTokenizer;

class Grid

{

privateint [ ][ ] mainGrid;

/*reads the file needed to determine sequence*/

public Grid( String file )throws IOException

{

FileReader fr =new FileReader( file );

Scanner scan =new Scanner( fr );

ArrayList<ArrayList><Integer>> numR =new ArrayList<ArrayList><Integer>>( );

while( scan.hasNextLine( ) )

{

String line = scan.nextLine( );

StringTokenizer st =new StringTokenizer ( line );

String number;

ArrayList<Integer> numC =new ArrayList<Integer>( );

while( st.hasMoreTokens( ) )

{

number = st.nextToken( );

int num = Integer.parseInt( number );

numC.add( num );

}

numR.add( numC );

}

int height = numR.size( );

int width = numR.get( 0 ).size( );

mainGrid =newint [ height ][ width ];

for(int i = 0; i < height; i++ )

{

for(int j = 0; j < width; j++ )

{

mainGrid [ i ][ j ] = numR.get( i ).get( j );

}

}

}

/*creates the Position of the number being read*/

privateclass Position

{

privateint row;

privateint col;

public Position(int r,int c )

{

r = row;

c = col;

}

public List<Position> getAdjacents( )

{

int lowRow = row != 0 ? row - 1 : 0;

int lowCol = col != 0 ? col - 1 : 0;

int highRow = row != mainGrid.length - 1 ? row + 1 : mainGrid.length - 1;

int highCol = col != mainGrid[ 0 ].length - 1 ? col + 1 : mainGrid[ 0 ].length - 1;

List<Position> result =new ArrayList<Position>( );

for(int r = lowRow; r <= highRow; r++ )

{

for(int c = lowCol; c <= highCol; c++ )

{

if( r != row || c != col )

result.add(new Position( r, c ) );

}

}

return result;

}

publicboolean equals( Object other )

{

if( ! ( otherinstanceof Position ) )

returnfalse;

Position rhs = ( Position ) other;

return row == rhs.row && col == rhs.col;

}

publicint getValue( )

{

return mainGrid [ row ][ col ];

}

public String toString( )

{

return"(" + row +"," + col +")";

}

}

public Position newPosition(int r ,int c )

{

returnnew Position( r, c );

}

/* Public driver sends the positions to the recursive driver*/

public List<Position> getSequence( )

{

List<Position> sequence =new ArrayList<Position>( );

for(int r = 0; r < mainGrid.length - 1; r++)

{

for(int c = 0; c < mainGrid[ 0 ].length - 1; c++ )

{

sequence = getSequence(new Position( r, c ));

}

}

return sequence;

}

/* Recursive routine determines the max subsequence*/

private List<Position> getSequence( Position pos )

{

List<Position> adj = pos.getAdjacents( );

if( adj.size( ) == 0)

{

List<Position> seq =new ArrayList<Position>( );

seq.add( pos );

return seq;

}

List<Position> maxSeq =null;

for( Position p: adj )

{

List<Position> currentSeq = getSequence( p );

if( currentSeq.size( ) < maxSeq.size( ) )

maxSeq = currentSeq;

}

maxSeq.add( pos );

return maxSeq;

}

/*prints the max subsequence*/

public String toString( )

{

StringBuffer sb =new StringBuffer( );

List<Position> sequence = getSequence( );

for( Position pos: sequence )

{

sb.append( pos.toString( ) +" with cost of " + pos.getValue( ) );

}

return sb.toString( );

}

}

class MaxSubsequence

{

publicstaticvoid main( String [ ] args )

{

try

{

long startTime;

long endTime;

startTime = System.currentTimeMillis( );

System.out.println(new Grid("numbergrid1.txt" ) );

endTime = System.currentTimeMillis( );

System.out.println("Elapsed time = " + endTime +" " +"ms");

}

catch( IOException e)

{

System.err.println("Error opening file");

}

}

}

Thanks in advance.

[9700 byte] By [applesorangesa] at [2007-11-26 20:02:28]
# 1

stack overflow happens when your recursive calls nest too deeply.

Either you have written the recursive call incorrectly (so it keeps calling itself and never stops) OR you have written it correctly and the problem just requires too many nested calls.

Usually the first reason is the problem, so you need to see why your logic failed and why you are calling it too many times

So at the front of each method foo(int a, int b) put in a print statements like

System.out.println("calling foo with" + a + ", " + b);

Now look at the output you get before it crashes. is it doing the right thing? If not, fix it.

marlin314a at 2007-7-9 23:01:42 > top of Java-index,Java Essentials,Java Programming...
# 2
That makes sense. Now I am going to see if I can test these out the correct way.Thank you.
applesorangesa at 2007-7-9 23:01:42 > top of Java-index,Java Essentials,Java Programming...