Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 14 of 14

Thread: recursion on initialization

  1. #1
    Junior Member
    Join Date
    Apr 2014
    Posts
    19
    My Mood
    Stressed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Arrow recursion on initialization

    i am trying to make a game, for some reason i have begun to get a java.lang.StackOverflowError.
    im not exactly sure how i can fix it. only removing line 14 from infopannel1 (and everything that used that class.) seems to work. im not sure what else i can do to fix it. or why its resulting in stack overflow for that matter.
    i am putting in a link for the files i wrote this using bluej
    (several classes have no relevance, errorv2, demonstration, folderreadertest, ReadWithScanner, saveloadtest, menutest,rannum, and menutestTester. are all irrelivent to my problem.)
    https://www.dropbox.com/sh/7f2u492aeitdrq0/Mt9BfNOcEK


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: recursion on initialization

    get a java.lang.StackOverflowError.
    Can you copy and paste here some of the stack trace showing where the recursive calls are being made.

    And post the code that that goes with where the recursive calling is being done.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Apr 2014
    Posts
    19
    My Mood
    Stressed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: recursion on initialization

    java.lang.StackOverflowError
    at failsafe.<init>(failsafe.java:9) ---> public class failsafe
    at mainmenu.<init>(mainmenu.java:16) -----> failsafe choice = new failsafe();
    at infopannel1.<init>(infopannel1.java:14)-----> mainmenu MM = new mainmenu();
    at mainmenu.<init>(mainmenu.java:18)-----> infopannel1 info1 = new infopannel1();
    at infopannel1.<init>(infopannel1.java:14)-----> mainmenu MM = new mainmenu();
    that is the code at each line. after that it goes from mainmenu.java:18 and infopannel1.java:14.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: recursion on initialization

    It looks like a tight loop. Each class's constructor is creating an instance of the other class.
    Can one class pass a reference to itself (this) to the other's constructor?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Apr 2014
    Posts
    19
    My Mood
    Stressed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: recursion on initialization

    im sorry, i dont quite understand.. the same style of loop as those two classes call appears several times without any such problem
    could you give me an example?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: recursion on initialization

    Post the code in the constructors for those classes. The example should be there.

    a calls b
    b calls a
    If you don't understand my answer, don't ignore it, ask a question.

  7. The Following User Says Thank You to Norm For This Useful Post:

    homey (April 29th, 2014)

  8. #7
    Junior Member
    Join Date
    Apr 2014
    Posts
    19
    My Mood
    Stressed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: recursion on initialization

    sorry. i may be a while. i have to get off[COLOR="Silver"]

    --- Update ---

    this is the first actual call and the preceding code
     
     mainmenu menu = new mainmenu();
            load Load = new load();
            save Save = new save();
            infopannel1 info1 = new infopannel1();
            infopannel2 info2 = new infopannel2();
            infopannel3 info3 = new infopannel3();
            printer print =  new printer();
            failsafe FS = new failsafe();
     
            menu.initialchoice();
    _____________________________________
    that calls
    public int mainav;
        failsafe choice = new failsafe();
        printer print = new printer();
        infopannel1 info1 = new infopannel1();
        infopannel2 info2 = new infopannel2();
        infopannel3 info3 = new infopannel3();
     
     public void initialchoice()
        {
            Scanner in = new Scanner(System.in);
            System.out.println("do you know what you're doing/ have a save file? \n if so, enter the number one");
            System.out.println("otherwise. enter the number two:");
            choice.initialchoiceTC();
        }
    ____________________________
    choice.initialchoiceTC(); calls
     
     public int mainavFS;
        public void initialchoiceTC()
        {
            Scanner in = new Scanner(System.in);
            while(mainavFS != 1 && mainavFS !=2)
            {
                try
                {
                    mainavFS = in.nextInt();
                    if(mainavFS < 1 || mainavFS > 2)
                    {
                        System.out.println("sorry, that wont work, try again please");
                    }
                }
                catch(InputMismatchException ex)
                {
                    System.out.println("incorrect or unrelated entry, please try again");
                    System.out.println("");
                    String flush = in.next();
                }
            }
     
        }
    _______________________________
    which is all that should happen. the print statements. and wait on user input.




    (the "_________________" separates different classses)
    Last edited by homey; April 29th, 2014 at 06:55 PM.

  9. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: recursion on initialization

    What class(es) is that code in?
    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  10. #9
    Junior Member
    Join Date
    Apr 2014
    Posts
    19
    My Mood
    Stressed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: recursion on initialization

    i separated them by class
    i gave a link to the folder, opened in blujay it contains all of them.
    do you want me to put the entirety of the involved classes in?

  11. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: recursion on initialization

    There are two classes that are of interest: mainmenu and infopannel1
    Where is the code for them and their constructors?

    BTW The class names don't follow the java standards: classnames should begin with an uppercase letter
    If you don't understand my answer, don't ignore it, ask a question.

  12. #11
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: recursion on initialization

    I can't quite make sense of the code posted in post #17, so I took the liberty of taking a peek at mainmenu.java and infopannel1.java in Dropbox based on the stack trace mentioned in post #3. The line numbers are slightly different from those in the stack trace, but I think I see the problem.

    In mainmenu.java:
    public class mainmenu
    {
        ...
        printer print = new printer();
        infopannel1 info1 = new infopannel1();
        ...
    In infopannel.java:
    public class infopannel1
    {
        ...
        failsafe choice = new failsafe();
        mainmenu MM = new mainmenu();
        ...
    The instance variable initialiser in each of the classes instantiates the other class. In the above case, during the instantiation of the mainmenu class, the instance variable initialiser, infopannel1 info1 = new infopannel1(), is executed. This results in the instantiation of the infopannel1 class, where the mainmenu MM = new mainmenu() instance variable initialiser is executed. Ad infinitum.

    To clarify this point, consider the example program below, which exhibit the same StackOverFlow error problem:
    public class InitRecursion {
        public static void main(String[] args) {
            ClassOne c1 = new ClassOne();
            System.out.println("Done");
        }
    }
     
    class ClassOne {
        private ClassTwo two = new ClassTwo();
    }
     
    class ClassTwo {
        private ClassOne one = new ClassOne();
    }
    If you run the above program, you'd get a StackOverFlowError with a very long stack trace.

    Note that when a class is instantiated, the new object is initialised by going through the following procedure (on a high-level):
    1. invoke another constructor in the same class (if such a call exists)
    2. invoke a superclass constructor
    3. execute instance initialisers and instance variable initialisers
    4. execute the rest of the body of the constructor

    (See Object initialization in Java | JavaWorld and Chapter*12.*Execution for elaboration on the above.)

    The above procedure is what <init> in "mainmenu.<init>" and "infopannel1.<init>" in the stack trace refer to. In your case, procedure #3 is what you're concerned with. infopannel1 info1 = new infopannel1() and mainmenu MM = new mainmenu() are the instance variable initialisers that are executed during object initialisation, and they are the source of the recursive calls that led to the StackOverFlowError.

    Quite advanced stuff, but the bottom line is don't instantiate each other in the instance variable declarations (nor in the constructors).

  13. The Following User Says Thank You to jashburn For This Useful Post:

    homey (April 29th, 2014)

  14. #12
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,198
    Thanks
    49
    Thanked 2,172 Times in 2,145 Posts

    Default Re: recursion on initialization

    taking a peek at mainmenu.java and infopannel1.java
    Thanks. Hard to get OPs to make that simple of a post.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #13
    Junior Member
    Join Date
    Apr 2014
    Posts
    19
    My Mood
    Stressed
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Default Re: recursion on initialization

    just to make sure i understand, which i believe i do, basically every time something such as
    class ClassOne {
        private ClassTwo two = new ClassTwo();
    }
    is typed, it creates an instance of ClassTwo, for the use of class ClassOne.
    thus what ive done is created two instances which infinitely call upon eachother. thus stack overflow.

  16. #14
    Member
    Join Date
    Feb 2014
    Posts
    180
    Thanks
    0
    Thanked 48 Times in 45 Posts

    Default Re: recursion on initialization

    Indeed!

    -Edit-
    To be more precise, they infinitely create each other, so you're effectively trying to create infinite (not two) instances of each of the classes.
    -End edit-
    Last edited by jashburn; April 29th, 2014 at 08:14 PM. Reason: Add clarification

  17. The Following User Says Thank You to jashburn For This Useful Post:

    homey (April 30th, 2014)

Similar Threads

  1. Initialization in class
    By bllnsr in forum Object Oriented Programming
    Replies: 3
    Last Post: November 12th, 2013, 12:32 PM
  2. Inner class initialization and declaration
    By longwu in forum Object Oriented Programming
    Replies: 2
    Last Post: August 31st, 2011, 07:44 AM
  3. Initialization of a class
    By Merik in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2010, 07:18 PM
  4. [SOLVED] JUnit initialization error
    By raphytaffy in forum Java IDEs
    Replies: 2
    Last Post: September 20th, 2010, 05:33 PM
  5. Initialization parameter of servlets
    By rakesh in forum Java Servlet
    Replies: 3
    Last Post: April 13th, 2010, 03:14 AM

Tags for this Thread