3 Replies - 1080 Views - Last Post: 09 June 2009 - 07:28 PM Rate Topic: -----

#1 Outdor  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 07-June 09

StackOverflowError

Posted 09 June 2009 - 05:55 PM

I am very new to Java so please excuse if this seams like a stupid question. I have a program using in a recursive algorithm to loop thru a block of code, and I am getting the following error.

Exception in thread "main" java.lang.StackOverflowError
at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:391)
at java.lang.StringBuilder.append(StringBuilder.java:119)
at city.routes(city.java:163)
at city.routes(city.java:168)

At first I thought that it was being caused by an infinite loop, but I stepped thru the code and it is triggering the error on the first pass. Here is my code, sorry to send teh whole program but I just don't know enough to narrow down the problem.

/*
 *Keith E. Coggin
 *outdor@outdorsrealm.net
 *AIM: KeithECoggin79
 *
 *Windows XP/Windows Vista
 *jGRASP CSD
 *Version 1.8.6_14
 *
 *IT310 - Data Structures and Algorthms
 *Unit 3 - Stacks, Queues, and Recursion
 *Sunday, June 7, 2009
 *
 *Academic Honesty:
 *I attest that this is my original work.
 *I have not used unauthorized source code, either modified or unmodified.
 *I have not given other fellow student(s) access to my program.
 */

//Inport Classes.
import java.io.*;
import java.util.*;

public class city
{

	//Properties
	String Name;
	int X;
	int Y;
	
	//Constructors
	public city()
	{
	
	}
	
	public city(String cName, int cX, int cY)
	{
		Name = cName;
		X = cX;
		Y = cY;
	}
	
	//Set Methods
	public void setName(String iName)
	{
		Name = iName;
	}

	public void setX(int iX)
	{
		X = iX;
	}
	
	public void setY(int iY)
	{
		Y = iY;
	}
	
	//Get Methods
	public String getName()
	{
		return Name;
	}
	
	public int getX()
	{
		return X;
	}
	
	public int getY()
	{
		return Y;
	}



	public static void main(String[] args)
	{
	
		//Create Variables.
		city tempCity = new city();
		LinkedList<city> cityList = new LinkedList<city>();
		
		//create a buffered reader to read input from the keyboard.
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

		//Initiate a try block to catch any errors for the streamreader object.
		try
		{
	
			//Initilize a loop to prompt the user for one of more additional cities.
			boolean cont = true;
			while (cont == true)
			{
			
				//Prompt the user for the next city.
				System.out.println("Please enter the next city(Type \"exit\" to quit.):");
				String tempCityName = in.readLine();
				if (tempCityName.equalsIgnoreCase("exit"))
				{
					cont = false;
				}
				else
				{
					tempCity.setName(tempCityName);
					System.out.println("Please enter the X cordinate for the next city:");
					tempCity.setX(Integer.parseInt(in.readLine()));
					System.out.println("Please enter the Y cordinate for the next city:");
					tempCity.setY(Integer.parseInt(in.readLine()));
					
					//Add the information entered to the appropriate stacks.
					cityList.add(tempCity);
				
				}
						
			}
		
		}
		
		//Catch block to complete try block.
		catch(IOException e)
		{
			System.out.println(e.getMessage());
		}
		
		routes(null, cityList, 1, 0, null);
		
	}

	public static void routes(city sCity, LinkedList<city> lCity, int count, int total, city firstCity)
	{
	
		//Declare variables.
		String path;
		double dist;
		city tempCity;
		
		//Test for top level, if yes run recursive.
		if (sCity == null)
		{
			sCity = firstCity = lCity.removeFirst();
			total = lCity.size();
			count = 0;
		}
		else if (count == total)
		{
			if (firstCity.equals(lCity.getFirst()))
			{
			
			}
			else
			{
				lCity.addLast(sCity);
				sCity = lCity.removeFirst();
				count = 0;
			}
		}
		else
		{
			tempCity = lCity.removeFirst();
			path = sCity.getName() + tempCity.getName();
			//enter equation here.
			lCity.addLast(tempCity);
		}
		
		routes(sCity, lCity, count, total, firstCity);
	}

}



Is This A Good Question/Topic? 0
  • +

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8021
  • View blog
  • Posts: 31,132
  • Joined: 06-March 08

Re: StackOverflowError

Posted 09 June 2009 - 06:37 PM

ho... the subtile one :D
Juicy as I like them :^:

OK your problem is here

		   tempCity.setName(tempCityName);
		   System.out.println("Please enter the X cordinate for the next city:");
		   tempCity.setX(Integer.parseInt(in.readLine()));
		   System.out.println("Please enter the Y cordinate for the next city:");
		   tempCity.setY(Integer.parseInt(in.readLine()));
					
		   //Add the information entered to the appropriate stacks.
			cityList.add(tempCity);



When you add an Object to a linked list you actually add a pointer to that object (shut not to loud Java programmers don't like the word pointer)

So when you add and add and add you always add the same city object to the linked list
I mean when you promp the user for the city name and you do:
tempCity.setName(tempCityName);
the name of the 10 city already in the linked list switch to your new city name
because assuming that tempCity is at address 016AF in memory what your linked list contains is
016AF
016AF
016AF
016AF
....
now when toi try to pass through the linked list .... the forward pointer is to 016AF which points to 016AF which......

better to
String name = in.readLine...
int x = ....
int y = .....
city c = new city(name, x, y);
cityList.add(c);



now a NEW object city will be added each time to the list
Was This Post Helpful? 1
  • +
  • -

#4 Fuzzyness  Icon User is offline

  • Comp Sci Student
  • member icon

Reputation: 669
  • View blog
  • Posts: 2,438
  • Joined: 06-March 09

Re: StackOverflowError

Posted 09 June 2009 - 06:37 PM

StackOverflow error when your using recursive means that somewher ein your code there is a recursive call that has no end. Thus uses up all your memory and crashes. It is similiar to an infinite loop
Was This Post Helpful? 0
  • +
  • -

#5 Outdor  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 22
  • Joined: 07-June 09

Re: StackOverflowError

Posted 09 June 2009 - 07:28 PM

I am now getting unique entrys in the linked list but now I am getting a null pointer exception as follows:

Exception in thread "main" java.lang.NullPointerException
at city.routes(city.java:142)
at city.main(city.java:128)

this is the updated section of code:

it is crashing at the last line in this segment.


Nevermind I just solved it.


-Outdor

This post has been edited by Outdor: 09 June 2009 - 07:32 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1