Strange Method Error

Discussion in 'Spigot Plugin Development' started by Bluerossman, May 10, 2015.

  1. Sorry for the ambiguous title, I'm not really sure what to call this..

    Anyway, I'm having a problem with this method:
        public Team getTeam(Player p) {
            for (Team t : teams) {
                if (t.getPlayers().contains(p)) return t;
            return null;
    For some reason it's always returning null in one class, but working absolutely fine in another.
    I instantiate the class containing this method in exactly the same way in both classes:
    GameHandler gh = new GameHandler();
    It works perfectly fine in this context:
    if (gh.getTeam(p) == null) {
        // Does code
    else p.sendMessage(prefix + ChatColor.GRAY + "You are already in a team!");
    And only returns the else statement when it's supposed to, but in the other CommandExecutor:
    if (gh.getTeam(p) != null) {
        // Does code
    else p.sendMessage(prefix + ChatColor.GRAY + "You must be in a team to join!");
    it always returns the else statement.
    Any ideas why?

    Also, whilst I'm here, what the convention for class placement of a new object?
    Should I have a package: me.bluerossman.object, and then have the class inside that, or put it inside me.bluerossman, or what?
  2. If you create multiple GameHandler instances in two classes, they won't share the data you add. Use dependency injection to pass a GameHandler instance to other classes, usually stored in your main class.
    • Winner Winner x 1
  3. Thankyou, I suspected it had something to do with instances.
    And my java incompetentness.