Solved Stack Overflow?

Discussion in 'Plugin Development' started by Raydond123, Feb 8, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    Raydond123

    I'm getting this error that I haven't gotten before with plugins using multiple classes. I'm relatively new using multiple classes so sorry if I'm a bit "nooby".
    First of, I'm getting a java.lang.StackOverflow error when my plugin starts up. Looking at the stack trace, these are the parts of the two files that are causing the problems.
    Countdowns (open)

    Code:
    Main plugin;
        LobbyHandler lobbyHandler;
        TeamDeathmatchHandler teamDeathmatchHandler;
       
        public Countdowns(Main plugin) {
           
            this.plugin = plugin;
            lobbyHandler = new LobbyHandler(plugin);
            teamDeathmatchHandler = new TeamDeathmatchHandler(plugin);
           
        }

    LobbyHandler (open)

    Code:
    Main plugin;
        Countdowns countdowns;
       
        public LobbyHandler(Main plugin) {
           
            this.plugin = plugin;
            countdowns = new Countdowns(plugin);
           
        }


    The console is just spammed with errors saying that the following lines of codes are causing the problem.
    Show Spoiler

    Code:
    LobbyHandler lobbyHandler;
    lobbyHandler = new LobbyHandler(plugin);
    
    Countdowns = countdowns;
    countdowns = new Countdowns(plugin);


    I'm confused as to why this is causing a problem in my plugin. Any ideas?

    Thanks in advance.
     
  2. Offline

    1Rogue

    You're creating objects in the constructor endlessly. Countdowns creates a new LobbyHandler, and LobbyHandler creates a new Countdowns, etc.

    You should have your main class create both instances, store them, and provide a getter for those values:

    Code:java
    1. public Countdowns getCountdowns() {
    2. return this.countdowns;
    3. }
     
  3. Offline

    Raydond123

    @1Rogue
    If I already have a constructor for the Main class in all of my classes could I just do plugin.[class-name here].
    Code:
        Main plugin;
       
        public Countdowns(Main plugin) {
           
            this.plugin = plugin;
           
        }
     
  4. Offline

    mythbusterma

    @Raydond123

    You didn't read his post carefully enough. Read it again and see your problem.
     
  5. Offline

    Raydond123

    @mythbusterma
    I see my problem but what I was asking if it's really necessary to have a getter method inside the Main class.
    Couldn't I just use the following to get access things in my other classes?
    Code:
    plugin.[classNameHere]
    Bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  6. Offline

    ZanderMan9

    In something like C#, yes, that would be normal convention, but in Java you're generally going to have a method to return the value rather than getting it directly.
     
  7. Offline

    Raydond123

Thread Status:
Not open for further replies.

Share This Page