Help with Constructor

Discussion in 'Plugin Development' started by ravand, May 14, 2014.

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


    I am trying to grab a HashMap from a different class and modify/read it in another.

    What i have:
    Main Class:
    1. if (args[0].equalsIgnoreCase("time")) {
    2. SetTime time = new SetTime(); //Constructor gives me ERROR here
    4. mapTime = time.getHashMap();
    5. player.sendMessage(mapTime.toString());
    6. return true;
    7. }

    SetTime Class:
    1. public class SetTime implements Listener {
    2. HashMap<Player, Boolean> map = new HashMap<Player,Boolean>();
    4. private Main plugin;
    5. public SetTime(Main plugin){
    6. this.plugin = plugin;
    7. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    8. }
    10. public HashMap<Player,Boolean> getHashMap(){
    11. return map;
    12. }

    Eclipse says "The Constructor SetTime() is undefined" so im guessing its because i already use the SetTime(Main plugin) to call my config files. As a suggestion it asks me to add an argument but when i chose that it turns it to "SetTime time = new SetTime(null);" and this returns a NullPointer when grabbing the HashMap.

    How should i rewrite this so i can still use the config files in the SetTime() class
    and also grab the HashMap inside the Main class from the SetTime() class.

    This might be a very stupid question appologizes in advance^^
  2. Offline

    Bailey Payne

    is the SetTime time = new SetTime(); the HashMap constructer for that class?

    if so try changing it to

    HashMap time = new HashMap();
    ravand likes this.
  3. Offline


    Your getting an error with your constructor as when your invoking the method your not passing the arguments, when the constructor has the 'Main' class as a parameter. Simply just create an empty constructor with no parameters to fix your issue.
    ravand likes this.
  4. Offline


    So your saying that adding
    1. public SetTime(){
    3. }

    would be enough? Or do i have to write anything into it?
  5. Offline


    Yeah that's all you need. Now you can call your getHashMap method.
    ravand likes this.
  6. Offline


    Yes you were right no more errors and i can call my getHashMap() method... However i noticed that the hashmap is empty when i try to call and convert it to string.

    So this:

    give me an empty "{}" list :/
    I tried sending the HashMap in the SetTime() class and it gave me the correct entries for it
    Any clue?
  7. Drew1080 Unless of course there was a reason for him asking for a parameter. As it turns out, there was!

    EDIT: He wasn't showing the whole code for SetTime. And I assume that the toString() method was for debugging purposes - to see why the map wasn't working. Makes sense to me.

    ravand Your HashMap is a non-static field which means that each instance has it's own map. The best way you can solve this is by using the same instance as the one where you do stuff with the map. You can also look into static fields but - especially if you don't fully know what you're doing - they are considered evil.
    ravand likes this.
  8. Offline


    The values are addid on a OnPlayerJoineEvent in the SetTime class. I have been working with them now without a problem just the cross Class usage is giving me a hard time right now

    I have tried calling a player Hash Value and now its giving me a null pointer

    1. HashMap<Player,Boolean> map = new HashMap<Player,Boolean>();
    2. map = time.getHashMap();
    3. player.sendMessage(map.get(player).toString());

    Creating HashValues in SetTime class:
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent e){
    3. Player p = e.getPlayer();
    5. map.put(p, false);
    6. }

    Am i doing wrong while assigning the 1 hashmap to the other maybe?

    Yes that was only for debugging purposes

    THey are non static yes but shouldn't i be able to apply 1 map from a different class to the main class's hashmap?
  9. ravand Your problem isn't that you're trying to access the map from a different class, your problem is that you're trying to access it from a different object. I suggest that you read some guides and learn more about OOP languages. :)
    ravand likes this.
  10. Offline


    dam^^ Could you give me an example of how it "could" work?

    EDIT: Ok seems like changing the hashmap in the SetTime class to public static fixed the issue :) can now read the values
  11. ravand An example? Not really. You can search for them though. What you do want to do though is look into one of the following, in no particular order:
    • Passing the instance as a parameter
    • Static factory methods
    • Static fields
  12. Offline


    All good changed it to public static and accessing it in a static way and now it works :) thanks all for your help
  13. ravand Exactly the way I wouldn't do it. :)
    ravand likes this.
  14. Offline


    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page