Results 1 to 5 of 5
  1. #1
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default Exception in thread "main" java.lang.StackOverflowError

    hi guys

    i have a recursive problem. I have two classes and i need to instantiate them mutually.

    so the result is the Stackoverflow error

    how i can solve that?

    my first class - King() (have more code, but is irrelevant for the case)

    Java Code:
    	final Rules result = new Rules();
    		final Cards gr = new Cards();
    
    		play2a = gr.GameRules2();
    		play2aa = gr.GameRules3();
    
    ActionListener five = new ActionListener() {
    			public void actionPerformed(ActionEvent e) {
    			pn10.setText(result.test());
    			}
    		};
    second class - Rules
    Java Code:
    public class Rules {
    	final King rules = new King();
    
    
    
    	public String test() {
    		if (rules.play2a > rules.play2aa) {
    			String result = "Player 1 get all cards";
    			return result;
    		} else {
    			String result_ = "Player two get all cards";
    			return result_;
    		}
    	}
    
    }

    thanks :)
    Last edited by felito; 01-31-2011 at 04:34 PM.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    You need to decide whether a King has Rules, or whether Rules have a King.

    If the King in Rules is simply supposed to be a reference to the one creating the Rules then don't create it there, simply pass in "this" to a Rules constructor:
    Java Code:
    final Rules result = new Rules(this);
    Of course this is based on almost no idea what your requirements are, since you haven't told us.

  3. #3
    felito is offline Member
    Join Date
    Nov 2010
    Posts
    48
    Rep Power
    0

    Default

    I tried but it didn't work

    i don't know how i can solve that...
    Last edited by felito; 01-31-2011 at 08:06 PM.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    What tolls is saying, is this.

    If all A's have a B and all B's have an A, how do you make the first A before you've made a B? Or, how do you make the first B without an A?

    This is a circular dependency. As Tolls said, something has to come first, otherwise you have a chicken and egg situation, and java creates stack entries until it overflows. Decide which class contains the other by definition - you can't have it both ways.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,802
    Rep Power
    19

    Default

    Well, they can have both , they just can't create both. My description was a bit off, it should have said "creates" rather than "has".

    But the rest stands. You need to decide who does the creating.

    Since a King seems to have Rules and the Rules are specific to the King (again, based on the amount of information you have supplied), then the King creates the Rules and the Rules accept a King reference.

    If you say you've tried but it didn't work then show us, because we can't explain where you've gone wrong without actually seeing it.

Similar Threads

  1. Replies: 5
    Last Post: 06-22-2010, 03:51 PM
  2. Replies: 3
    Last Post: 06-01-2010, 03:01 PM
  3. Replies: 1
    Last Post: 11-20-2009, 07:58 PM
  4. Replies: 6
    Last Post: 07-16-2009, 03:30 PM
  5. Replies: 1
    Last Post: 07-16-2009, 10:35 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
  •