Solved Why would a HashMap be empty when i've set it?

Discussion in 'Spigot Plugin Development' started by NavyDev, Apr 19, 2017.

Thread Status:
Not open for further replies.
  1. So in a plugin i'm making I use hashmaps for finding out information, as most people would.

    However, I have no idea why a hashmap would go from having at least one key and value in it, to nothing.
    I've tried my own form of debugging which is where I got what I know so far.

    How my plugin works:

    I add values to the hashmap through an event. These values are the ball UUID and the MATCH it is currently handling.
    I then broadcast the hasmap in the same void. It returns: "UUID, MATCH" - Perfect.

    I then move to a different void and tried try to get the match using the balls UUID. Nothing.
    So I test why, by broadcasting the whole hashmap. Empty.

    So in chat this would look like:

    Event - "UUID, MATCH"
    GOAL - "" - Same hashmap

    I don't set the hasmap as nothing anywhere so it's probably not that


    Any ideas?
     
  2. You're using two different instances of the same class. Use the same instance instead.

    (For more in depth help, you'd need to post some code)
     
    • Like Like x 1
  3. It's in the same class boss, sorry I should of said that.

    El code:
    Hashmap being declared:
    Code (Text):
    public HashMap<UUID, Match > BMatch = new HashMap<UUID, Match>();

    Event (cut down to the relevent parts):
    Code (Text):
    @ArenaEventHandler
        public void onPlayerAnimation(PlayerInteractEvent event) {
        Player player = event.getPlayer();
            for (Entity entity : ent) {
                if (entity instanceof Item && canKick.contains(kickersTeam)) {
                    UUID Buid = entity.getUniqueId();
                    BMatch.put(Buid, match);
                    Bukkit.broadcastMessage(BMatch.toString());
                    Bukkit.broadcastMessage(match.getArena().toString());
                    }
    Goal:
    Code (Text):

        public void onGoalScored(Material material, Entity ent, UUID Buid) throws IOException, InvalidConfigurationException
          {
            Bukkit.broadcastMessage(Buid.toString()); // (Retuns the same as the previous BUID (Works))
            Bukkit.broadcastMessage("KickedBy " + kickedBy.toString()); // (Returns "KickedBy {}")
            Bukkit.broadcastMessage("BMatch " + BMatch.toString()); // (Returns "BMatch {}")

            thisMatch = BMatch.get(Buid);
            Bukkit.broadcastMessage("Match: " + thisMatch.toString()); //- Stops here (Error, since it can't find the match)
              World world = ent.getWorld();
              Location center = fixCenter(world,
                match.getSpawn(2, false).getLocation());
              }
    Perhaps my hashmap should be private? Nope, didnt work
    Guess you can't use hasmaps across methods...
     
    #3 NavyDev, Apr 19, 2017
    Last edited: Apr 19, 2017
  4. Right, sometimes you just need to have a little post to spigot to get a new perspective.

    Thank you @DarkSeraphim just worked out what you meant.

    By me using multiple instances of my class (in order to use it across other classes, over static references) I am creating 2 hashmaps, one with my values and one without. Therefore limiting the hashmap to the method its variables are put in.

    For anyone with the same problem, it's a simple fix. Just change your declaration from this:
    Code (Text):
    public HashMap<UUID, Match > BMatch = new HashMap<UUID, Match>();
    To this:
    Code (Text):
    private static HashMap<UUID, Match > BMatch = new HashMap<UUID, Match>();
    Cheers,
    Navy
     
    • Optimistic Optimistic x 1
  5. I don't think you understand what you're doing...

    Learn how Objects references and static works.
     
    #5 Necrone, Apr 19, 2017
    Last edited: Apr 19, 2017
    • Agree Agree x 1
  6. Unneeded post really, not at all relevant. Just made to make me feel bad (not that it did). Not sure, why. I don't think i've done anything to you?

    Regardless. Have a good day sir!

    P.s this post is finished, I did actually work out why it wasn't working. For someone who his self taught I don't think i've done at all bad. I learn as I go and there's nothing wrong with asking for help now and then! I even gave the solution for anyone in the future who needs it!
     
    • Optimistic Optimistic x 1
  7. That's not really a solution - it's a problem that doesn't occur in the first place when you are aware of how variable scopes work. Also, it's not a personal attack, it's a serious suggestion that you take some time to learn how things work properly, and save yourself the time several times over from bugs that arise in future.
     
    • Agree Agree x 1
  8. I finally solved something pretty teasing in my code; the problem, though, wasn't determined by having the object declared public or private: it was having it declared static or not.
     
  9. FrostedSnowman

    Resource Staff

    instead of static, u could just create one instance of that class, and use getters/setters to retrieve the map and its data


    Code (Java):

    public class Example {

    private Map<String, String> strings = new HashMap<>();

    public Map<String, String> getStrings() {
              return strings;
    }

    //other getters;setters

    public class Main extends JavaPlugin {
         private  Example example;

         public void onEnable() {
              example = new Example();
         }

         public Example getExample() {
              return example;
         }
    }

    public class Example2  {

         public void test() {
              MainInstance().getExample().put("key", "value");
         }
    }
     
    • Agree Agree x 1
    • Funny Funny x 1
Thread Status:
Not open for further replies.