Hello there. I am writing a game in java for a class of mine but i am getting this bugger ass error and i cant find it. Can someone help me? The whole project including all classes and such is located in the .zip you can download. I swear it doesn't have any virus's I just really need some help.

Recommended Answers

All 13 Replies

Member Avatar for ztini

I swear it doesn't have any virus's I just really need some help.

Yeah ok, I swear I believe you.

Post your code.

Its in the .zip folder. There are 3 classes in the src folder that i think are errored for some reason.

basically, the stack is the 'heap' on which all your (local) variables are created.
if you never destroy or re-use any, just keep creating new ones, the time will come that your (limited) space on the heap is maxed out. if then, you're creating one more.

bam.

stack overflow.

so, you'll have to check your code.. are there nowhere objects you can set to null because they're no longer used. do you have to create new instances/primitives, or are there some you can reuse without creating a new one.

also: are you sure you don't come into an infinite loop in which you are creating variables?

a few things you may want to check first :)

Yah im not really sure if im creating some infinite loops. And how do i set loops or variables to null after im done using them?

String a = new String("still needed");
a = null; // not used anymore

I dont even have many variables. Most of thema re if statements. They look like this

if (apartmentChoice == 1){
			itemDescription.journal();
		}
		if (apartmentChoice == 2){
			itemDescription.nightStand();
		}
		if (apartmentChoice == 3){
			itemDescription.door();
		}
		if (apartmentChoice == 4){
			itemDescription.lightSwitch();
		}
		if (apartmentChoice == 5){
			itemDescription.shelves();
		}
		if (apartmentChoice == 6){
			itemDescription.bed();
		}
		if (apartmentChoice == 7){
			itemDescription.food();
		}
		if (apartmentChoice == 8){
			itemDescription.beerCans();
		}
		if (apartmentChoice == 9){
			location.apartmentSecondRoom();
		}

does that work? Or are all of these messing me up?

don't know. I see you calling a lot of methods, but I have no idea of what they do.
also, those if-s might be in a loop, which could be part of it.

best thing to do is post the entire code.

Hang on... this is a stack overflow, not a heap overflow. All the discussion so far applies to a heap overflow. Objects are created on the heap, and you overflow that by creating too many objects, or by preventing old objects from being garbage collected.
The stack is where blocks such as method calls and their local variables (ie primitives and references - no objects) are stored. and the stack increases when you enter a block, and reduces again when you leave it. Pretty much the only way to create a stack overflow in normal code is to call one or more methods recursively - ie keep entering blocks but never leave them.
Look for a method that calls itself, or a number of methods that do that in a cycle.
If you get a stack trace that will show you exactly where the recursion is.

commented: ++ +14

I think the above poster might have just fixed it for me. BUT here are all 3 classes i am working on. See if you can help me pin point what i did wrong.

theMainThingy

public class theMainThingy {
	
public static void main(String[] args){
		
		places location = new places();
		
		location.apartment();
		}
		
	}

places

import java.util.Scanner;

public class places {
	
	Scanner reader = new Scanner(System.in);
	items itemDescription = new items();
	places location = new places();
	
	public void apartment(){
		
		
		int apartmentChoice;
		
		System.out.println("You slowly begin to wake up. Your mind is fuzzy and    you dont");
		System.out.println("remember anything that happened the previous night. You cant even remember your name.\n");
		System.out.println("In your stupor you fall over, stub your toe, and jump up in pain.\n");
		System.out.println("FINALLY awake, you look around the room, tryin to figure out something, anything, \n" +
				"to hopefully determine at the very least who you are.\n");
		System.out.println("\n As you look around the room you notice a (1)journal next to your (2)nightstand.\n" +
				"There is a brown (3)door in front of you. Next to the door there are two (4)light switches.\n" +
				"There are several (5)shelves in front of you next to what appears to be a sudo-library. \n" +
				"The (6)bed you woke up on is rumpled to all hell. There are (7)plates of food on the table to the left of you.\n" +
				"(8)Beer cans are scattered along the other edge of the bottom of the bed. Another (9)room is visible from where you are standing.\n" +
				"It is down the hallway a good 15 feet from where you are standing.\n");
		System.out.print("Examine Object Number: ");
		
		apartmentChoice = reader.nextInt();
		
		if (apartmentChoice == 1){
			itemDescription.journal();
		}
		if (apartmentChoice == 2){
			itemDescription.nightStand();
		}
		if (apartmentChoice == 3){
			itemDescription.door();
		}
		if (apartmentChoice == 4){
			itemDescription.lightSwitch();
		}
		if (apartmentChoice == 5){
			itemDescription.shelves();
		}
		if (apartmentChoice == 6){
			itemDescription.bed();
		}
		if (apartmentChoice == 7){
			itemDescription.food();
		}
		if (apartmentChoice == 8){
			itemDescription.beerCans();
		}
		if (apartmentChoice == 9){
			location.apartmentSecondRoom();
		}
		else{
			
		}
		
		return;
	}
	
	int apartmentOtherRoomItemSelection;
	
	public void apartmentSecondRoom(){
		System.out.println("There is a (1)book laying on the ground, spine bent in half but easily still readable.\n " +
				"A (2)Subway uniform is laying crumpled in a dirty clothes hamper.\n " +
				"An (3)answering machine with a glowing red light is blaring about 4 feet from an xbox.\n " +
				"It also looks like there is a (4)golden object obscured by some papers on a dresser to my right.");
		System.out.print("Where should I start?: ");
		apartmentOtherRoomItemSelection = reader.nextInt();
		
		if (apartmentOtherRoomItemSelection == 1){
			itemDescription.bookOnMasterkey();
		}
		
		if (apartmentOtherRoomItemSelection == 2){
			itemDescription.nameTag();
		}
		
		if (apartmentOtherRoomItemSelection == 3){
			itemDescription.answeringMachine();
		}
		
		if (apartmentOtherRoomItemSelection == 4){
			itemDescription.watch();
		}
		
	
		
		return;
	}

}

items

import java.util.Scanner;

public class items {
	
	Scanner reader = new Scanner(System.in);
	places location = new places();
	
	public void journal(){
				
		
		System.out.println("You flip through the pages of a tattered book next to the bed. All but 3 pages appear to have\n" +
				" been destroyed by water damage. The entry dates are marked 12/24/13, 12/25/13, and 12/26/13.\n" +
				"I should read page 1, page 2, and page 3 of this journal.\n");
		
		int journalPage = 0;
		
		System.out.print("What page would you like to read?: ");
		journalPage = reader.nextInt();
		
		if (journalPage == 1){
		System.out.println("\n12/24/13: Marco is coming over later and we were gonna hit up a Christmas Eve Party.\n" +
				"Should be pretty fun. Martha is supposed to be there. Im getting pumped!\n");
		System.out.println("Umm i wonder if Marco is my friend? And who is Martha?\n");
		journalPage = 0;
		System.out.print("What page would you like to read?: ");
		journalPage = reader.nextInt();
		}
		
		if (journalPage == 2){
		System.out.println("\n12/25/13: Oh man I got so hammered yesterday. I made an ass out of myself in front of Martha. \n" +
				"I asked if I could make it up to her over Coffee. She said only if I was sober. Time to wash up and play some Minecraft.\n");
		System.out.println("Well this is an interesting turn of events. What could the next day possibly say?\n");
		journalPage = 0;
		System.out.print("What page would you like to read?: ");
		journalPage = reader.nextInt();
		}
		
		if (journalPage == 3){
		System.out.println("\n12/26/13: ......" +
				"All I can make out is Back, Van, and Chatham. What the hell is this?");		
		System.out.print("I should go back to the main room. Go back to main room?(y/n): ");
		
		return;
		}	
				
		}
		
		public void nightStand(){
			System.out.print("The usual stuff, underwear, pants, shorts, condoms, and........what the hell?\n" +
				" There is a gun in here? Shit since when did I own a gun? Hmmm.....I have no idea.");
		return;
		}		

		public void door(){
		System.out.print("The door is locked. This is kind of weird that im locked inside an apartment. " +
				"Maybe i should try and find my way out. I could try and use something i find during my search for who I am.");
		return;
		}
	
		public void lightSwitch(){
		System.out.print("I dont think so....im afraid of the dark.");
		return;
		}
	
		public void shelves(){
		System.out.print("Well it seems I have a bo of fruitloops next to the original star wars trilogy. Happy me!");
		return;
		}
	
	public void bed(){
		System.out.print("Maybe i should find out my name before i got to sleep. ");
		return;
	}
	
	public void food(){
		System.out.print("This is disgusting. Am i really this messy? ");
		return;
	}
	
	public void beerCans(){
		System.out.print("Maybe i just got hammered last night. ");
		return;
	}
	
	public void bookOnMasterkey(){
		System.out.print("Hmmmm this is strange. Theres a book here called Guns & Accessories. Lets check it out..." + 
				"\n\nChapter 12: Shotguns" +
				"\nThe masterkey is a door-breaching gun system." +
				"\n\nIf I can find a gun around here, maybe i can open that door");
		return;
	}
	
	public void nameTag(){
		System.out.print("Its a Subway employee nametag. \nAll it says is Bradley...");
		return;
	}
	
	public void answeringMachine(){
		System.out.print("Lets see if theres any messages that can be of any use to me..." +
				"\n\nHey honey! Im just calling to chat. Oh and i wanted to tell you to take a shower! " +
				"\nGirls like good hygene. Dont forget that! " +
				"Ok well call me back when you get this message! Ok, i love you! Byebye!");
		return;
	}
	
	public void watch(){
		System.out.print("It says today is the 27th of December. \n Well i guess im making some progress.");
		return;
	}
}

Yup, standard mistake number 41267 in the catalog of standard mistakes:

public class places {
   ...
   places location = new places();

Your places (should be Places) class has an instance variable "location" that is initialised to a new Place.
The first time you create a new Place its instance variables are initialised, including location, which is initialised to a new Place, whose instance variables include location, which is initialised to a new Place, whose instance variables include location, which is initialised to a new Place, whose instance variables include ... until you get a stack overflow.

WOW haha i get it. But now how exactly do i fix the error? I tried deleting the line of code but it still is giving me the overflow error.

Just kidding i had to delete Places location = new Places(); from my items class as well. Cheers and thanks for all the help! I really appreciate it! Ill make sure to post the full game when im actually done lol.

How you fix it depends on what that variable was for in the first place; you know that but I don't, so all I can say is don't initialise any Places instance variables to new Places().
Your error message for the exception tell you the class, method, and line number where the error was detected. You have that info, but I don't.

(posted before I read your last post)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.