Results 1 to 3 of 3
  1. #1
    calle is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default 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.


    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;
    	    }
    	}
        }
    Any help is good :)
    Cheers!



    The two methods is inside the Person
    Heres the code for the classes/objects im referring to in the above code:
    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;
        }
    The Person class got several methods, so only posted the varables from that one.


    Java Code:
    class Friend {
        Friend nextFriend;
        Person personObject;
        
        Friend (Friend nextFriend, Person personObject) {
    	this.nextFriend = nextFriend;
    	this.personObject = personObject;

  2. #2
    Zepher is offline Member
    Join Date
    Feb 2011
    Posts
    15
    Rep Power
    0

    Default

    It looks to me like in your while statement:
    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;
    }
    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.

    Double check the intent of this while loop and I think you will find a solution that fits your needs.

    Good Luck.

  3. #3
    calle is offline Member
    Join Date
    Feb 2011
    Posts
    2
    Rep Power
    0

    Default

    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

  1. help needed in runnin java project
    By anurag.25 in forum Advanced Java
    Replies: 14
    Last Post: 02-15-2009, 08:00 PM
  2. Replies: 0
    Last Post: 02-01-2009, 09:43 AM
  3. Replies: 2
    Last Post: 08-21-2008, 08:33 PM
  4. data from the main/GUI thread to another runnin thread...
    By cornercuttin in forum Threads and Synchronization
    Replies: 2
    Last Post: 04-23-2008, 11:30 PM
  5. Out of memory
    By mew in forum New To Java
    Replies: 1
    Last Post: 01-20-2008, 09:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •