Results 1 to 3 of 3
- 02-09-2011, 09:02 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Why am i runnin out of memory... ?
Im trying to figure out why i get the out of memory error when running.
The addFriend method works fine, but when i add the addFriendBack and run the program and activate the addFriend method my program crashes.
Any help is good :)Java Code:public boolean addFriend(String name, String friendName) { Person gainFriend = getPerson(name); Person newFriend = getPerson(friendName); if (gainFriend == null || newFriend == null) { //Checking if both ppl exists System.out.println("One or both of the names is not registered"); return false; } Friend friendCheck = gainFriend.firstFriend; //Adds friend here if one was friendless if (friendCheck == null) { gainFriend.firstFriend = new Friend(null, newFriend); gainFriend.firstFriend.personObject = newFriend; System.out.println(friendName+" was succesfully added to "+name+"'s friends"); addFriendBack(gainFriend, newFriend); return true; } else { while (friendCheck != null) { if (friendCheck.personObject.equals(newFriend)) { System.out.println(name+" is already friends with "+friendName); return false; } else { if (friendCheck.nextFriend == null) { //Adding new friend at the end of the chain friendCheck.nextFriend = new Friend(null, newFriend); friendCheck.nextFriend.personObject = newFriend; System.out.println(friendName+" was succesfully added to "+name+"'s friends"); addFriendBack(gainFriend, newFriend); return true; } } friendCheck = friendCheck.nextFriend; } } return false; // ================DO OVER } public void addFriendBack(Person gainFriend, Person newFriend) { Friend friendCheck = newFriend.firstFriend; //Adds friend here if one was friendless if (friendCheck == null) { newFriend.firstFriend = new Friend(null, newFriend); newFriend.firstFriend.personObject = newFriend; } else { while (friendCheck != null) { if (friendCheck.nextFriend == null) { //Adding new friend at the end of the chain friendCheck.nextFriend = new Friend(null, newFriend); friendCheck.nextFriend.personObject = newFriend; } friendCheck = friendCheck.nextFriend; } } }
Cheers!
The two methods is inside the Person
Heres the code for the classes/objects im referring to in the above code:
The Person class got several methods, so only posted the varables from that one.Java Code:class Person { String name; String number; Person next; Friend firstFriend; private int numberOfFriends; Person(String name, String number) { this.name = name; this.number = number; }
Java Code:class Friend { Friend nextFriend; Person personObject; Friend (Friend nextFriend, Person personObject) { this.nextFriend = nextFriend; this.personObject = personObject;
- 02-10-2011, 12:01 AM #2
Member
- Join Date
- Feb 2011
- Posts
- 15
- Rep Power
- 0
It looks to me like in your while statement:
you will loop infinitely because friendCheck (the object) will never be null because it was initialized at the beginning of your method. Because of this, you continue adding friends until you run out of memory.while (friendCheck != null) {
if (friendCheck.nextFriend == null) { //Adding new friend at the end of the chain
friendCheck.nextFriend = new Friend(null, newFriend);
friendCheck.nextFriend.personObject = newFriend;
}
Double check the intent of this while loop and I think you will find a solution that fits your needs.
Good Luck.
- 02-10-2011, 09:10 AM #3
Member
- Join Date
- Feb 2011
- Posts
- 2
- Rep Power
- 0
Cheers!
Thank you for making me notice the infinite loop!
I think the reason for me not seeing it was that in the "main" addFriend method im using the same while sentence, but im able to break out of it coz of the return statements. So I dont know if this is the right way to correct it but i just made the addFriendBack method boolean aswell, so i could break out of the loops with return.
Java Code:public boolean addFriendBack(Person gainFriend, Person newFriend) { Friend friendCheck = newFriend.firstFriend; if (friendCheck == null) { newFriend.firstFriend = new Friend(null, gainFriend); //Adds friend here if one was friendless System.out.println("DWADAWDAWD"); //REMOVE REMOVE REMOVE return true; } else { while (friendCheck != null) { if (friendCheck.nextFriend == null) { //Adding new friend at the end of the chain friendCheck.nextFriend = new Friend(null, gainFriend); System.out.println("HMM!"); //REMOVE REMOVE REMOVE return true; } friendCheck = friendCheck.nextFriend; // THINK THINK THINK } } return false; } }
Similar Threads
-
help needed in runnin java project
By anurag.25 in forum Advanced JavaReplies: 14Last Post: 02-15-2009, 07:00 PM -
Eclipse failing to start on Vista 64bit Home Prem.. while i got no prob runnin JGrasp
By kendy in forum EclipseReplies: 0Last Post: 02-01-2009, 08:43 AM -
how do I increase memory allocated to code cache (Non Heap Memory)
By manibhat in forum Advanced JavaReplies: 2Last Post: 08-21-2008, 07:33 PM -
data from the main/GUI thread to another runnin thread...
By cornercuttin in forum Threads and SynchronizationReplies: 2Last Post: 04-23-2008, 10:30 PM -
Out of memory
By mew in forum New To JavaReplies: 1Last Post: 01-20-2008, 08:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks