Solved Unable to access hashmap from separate class

Discussion in 'Plugin Development' started by HeyAwesomePeople, Nov 19, 2014.

Thread Status:
Not open for further replies.
  1. Hello friends,

    I am coding and have run across and issue. I have a class, named AdminCommands, that contains the HashMap<UUID, Integer> editingMap. One of the lines in the class puts the player uuid and integer of the map they are trying to edit.

    This works fine and dandy and all, but when I am calling the hashmap from a separate class apart from AdminCommands, it returns an empty map. But calling the hashmap in the AdminCommands class works.

    I have an instance of the AdminCommands class in my main class, using AdminCommands admincommands = new AdminCommands();. I then call plugin.admincommands.editingMap to find the hashmap. I have done it this way for years and it has worked perfectly until now.

    Main class: http://hastebin.com/idogorilid.avrasm (Line 30 and 42 is the main instance, Line 35 and 49 is the AdminCommands instance)
    AdminCommands class: http://hastebin.com/fonogayumi.avrasm (Line 166)
    Class I am trying to call the hashmap from: http://hastebin.com/uherohudic.avrasm (Line 49 is a test to show the hashmap, return empty no matter what)

    Why can I not access the hashmap from another class?
    Thanks
     
  2. Offline

    pookeythekid

    HeyAwesomePeople I'm no expert with statics--a year and counting with Java (Bukkit plugin) experience, and I still don't fully understand them--but I think your problem is that your HashMap is not static. With each instance of the AdminCommands class that you create, you're creating an empty HashMap with that instance. If you try to access your new instance, it's going to return an empty HashMap. You have to use the same instance of the class throughout the plugin.

    Sorry if this doesn't really tell you anything and you are using the same instance throughout the plugin; I kinda scanned over the code, I may not have read your post very clearly, and I'm a bit tired.

    Edit: Looking over your AdminCommands class a little bit more, I think that the above explanation may likely be the case.

    teej107 Thank you. I do know one thing, though, which gave me any idea that it could be useful information: a static object belongs to the class, not each instance of the class. It will not have different values according to different instances.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Nov 27, 2017
  3. Offline

    teej107

    es359 likes this.
  4. Offline

    Crud41


    That is almost exactly the same thing I told him when I was taking to him earlier today.
     
  5. Offline

    mythbusterma



    Please, for the love of god, stop trying to get people to abuse static. It's incredibly annoying and wastes valuable time that could be used to debug issues that aren't related to poor coding practise.
     
  6. Offline

    teej107

    static fields belong to the class, not the instance. Using static destroys OOP. The good rule to follow when using static is that they should be primitives (or String) and should be constants.
     
    es359 likes this.
  7. Offline

    pookeythekid

    Edit: Got ninja'd. ^ Re-edit: You just restated exactly what I said...

    *sigh* This is where teej107 's signature comes into action...

    HeyAwesomePeople Here's the way I do things in my plugins--and it's not with statics. I put any HashMaps, ArrayLists, etc. that I want to use in more than one class in my main class. I can't give you any expert's explanation on how that works any different than putting it in a different class--in fact I wouldn't mind at all if someone told me the difference (without irritated comments of superiority)--but it works fine for me.
     
  8. Offline

    mythbusterma

    pookeythekid

    That's how you're supposed to do it.....I suppose you could put it in a "Manager" class as well, but it doesn't really matter.
     
  9. Offline

    teej107

    You pass one instance in the parameters of methods or constructors and you have access to everything you need (in your case). However learn how to use getters. In fact, follow my advice given above
    EDIT: I usually have HashMaps, Collections, etc in the class they most belong to and use getters to get the class in your JavaPlugin class. Basically the same thing though.
     
  10. Offline

    pookeythekid

    teej107 Thank you. And that one isn't sarcastic.
     

  11. mythbusterma You are all very helpful, and I thank you for having a rant about static(even though I would never use it, unless i needed to. I learned my lesson about static). But the thing is is that this same exact method works on other classes. For example, in my AdminCommands class, I also call plugin.mapmanager.maps, which is a hashmap, and it doesn't return null. It returns what it is supposed to. This means I am able to get hashmaps from any class out of the MapManager class, but not AdminCommands class.. Why is this? I have tried using a getter and setter, same result, map is empty.
    The way I am doing things right now is how I have been doing it with no problem in other plugins and even classes in the plugin I am working on right now. There is obviously some sort of human error that I am just unable to see past at the moment.

    Based on what I am doing, I am creating a total of one instance of AdminCommands and editing the hashmaps in that one instance.

    I will try reorganizing how my code it setup, using a manager for things list maps. Might fix some issues.

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

    Skionz

  13. Offline

    ChipDev

    Static? Correct me if I'm wrong. I would like to know otherwise.
     
  14. Offline

    567legodude

    HeyAwesomePeople This is how I do it.
    In the main class
    Code:java
    1. HashMap<UUID, Integer> name = new HashMap<UUID, Integer>();

    in the class you want
    Code:java
    1. main plugin;
    2.  
    3. public nameOfThisClass(main plugin) {
    4. this.plugin = plugin;
    5. }

    and how you access it in that class.
    Code:java
    1. // Wherever you need the hashmap, just use this.
    2. plugin.name....
    3.  
    4. // Example
    5. plugin.name.put(UUID, Integer);
     
    MordorKing78 likes this.
  15. Offline

    mythbusterma

    567legodude

    No. No. No. No.

    Use get methods, don't just leave it public (or package-protected).
     
  16. mythbusterma 567legodude Okay I decided to clean up a little and move all hashmaps into a sort of manager class. Everything works perfectly now, so either my coding layout was wickedly messed up or something was really wrong. But thanks for the help guys!
     
    mythbusterma likes this.
Thread Status:
Not open for further replies.

Share This Page