Results 1 to 6 of 6
  1. #1
    zaricpp is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default Gui problem and stack overflow error

    In the GUI of my program, I need a method in a different class called when a button is clicked, and then that method changing the text of a jLabel in the GUI. NetBeans says my code has no errors, but when I create the object to call the function, the compiler says "Stack Overflow error" and the GUI won't even run. I don't understand why.

    I am sure that the object is the problem because when I comment out the object and calling the method, my GUI will run.
    Java Code:
    private CardManager cardManagerObject = new CardManager();
    ...
    private void newCardButtonActionPerformed(java.awt.event.ActionEvent evt) {                                              
            cardManagerObject.addCard();
        }
    Then in the other class:
    Java Code:
    public class CardManager {
        private FlashCardGUI guiObject = new FlashCardGUI();
        public void addCard(){
        guiObject.jLabel1.setText("helloo"); //Just to test that it's working properly
    }
    If I could just be pointed in the right direction as to how to fix this error it'd be greatly appreciated. Thanks :)
    Last edited by zaricpp; 06-03-2011 at 03:59 AM. Reason: added detail

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You've got an infinite recursion going on somehow somewhere, but I can't see where though based on the code that you've posted.

  3. #3
    zaricpp is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    You've got an infinite recursion going on somehow somewhere, but I can't see where though based on the code that you've posted.
    That's what I thought. I looked up stack overflow error and it said that I would have an infinite recursion but I cant find it either. But I don't understand because when I comment out the object and method, it works fine.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by zaricpp View Post
    That's what I thought. I looked up stack overflow error and it said that I would have an infinite recursion but I cant find it either. But I don't understand because when I comment out the object and method, it works fine.
    Is that the whole method that you've posted? or does it do other things?

    If you want our help though, it should be obvious that you'll need to post more code.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I do see one problem, your GUI makes a new CardManager object and the CardManager object makes a new GUI object and that won't do and can cause recursion. To clarify, here's what happens:

    • The GUI is create and makes a new CardManager object
    • The CardManager then creates a new GUI object which
    • creates a new CardManager object which
    • creates a new GUI object which
    • creates a new CardManager object which
    • creates a new GUI object which
    • creates a new CardManager object which
    • creates a new GUI object which
    • .... get the picture?


    I think that the GUI should make the CardManager and the CardManager not make the GUI but rather get the GUI instance passed into it via its constructor:

    Java Code:
    public class CardManager {
        private FlashCardGUI guiObject;
    
        public CardManager(FlashCardGUI guiObject) {
            this.guiObject = guiObject;
        }
    
        public void addCard(){
            guiObject.jLabel1.setText("helloo"); //Just to test that it's working properly
        }
    }
    Last edited by Fubarable; 06-03-2011 at 04:39 AM.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,010
    Rep Power
    20

    Default

    Als, your StackOverflow error message will have a stack trace with it, which will show you where the recursion is.
    Something like this:
    Exception in thread "main" java.lang.StackOverflowError
    at OopsRecursion2.<init>(Scratch.java:338)
    at OopsRecursion.<init>(Scratch.java:334)
    at OopsRecursion2.<init>(Scratch.java:339)
    at OopsRecursion.<init>(Scratch.java:334)
    etc etc.

Similar Threads

  1. Preventing stack overflow?
    By bobocheez in forum New To Java
    Replies: 11
    Last Post: 01-05-2011, 05:50 PM
  2. Replies: 1
    Last Post: 03-17-2010, 05:25 AM
  3. Stack Overflow work around?
    By Coukapecker in forum New To Java
    Replies: 2
    Last Post: 03-14-2010, 08:49 PM
  4. Java Stack Overflow?
    By fullmetaljacket in forum New To Java
    Replies: 0
    Last Post: 05-19-2009, 07:49 PM
  5. Graphics2D: stack overflow error
    By rosh72851 in forum New To Java
    Replies: 11
    Last Post: 10-15-2008, 09:01 PM

Posting Permissions

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