Help with Constructor

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

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

    ravand

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

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


    SetTime Class:
    Code:java
    1. public class SetTime implements Listener {
    2. HashMap<Player, Boolean> map = new HashMap<Player,Boolean>();
    3.  
    4. private Main plugin;
    5. public SetTime(Main plugin){
    6. this.plugin = plugin;
    7. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    8. }
    9.  
    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^^
    Thanks
    ravand
     
  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

    Drew1080

    ravand
    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

    ravand

    @Drew1080
    So your saying that adding
    Code:java
    1. public SetTime(){
    2.  
    3. }

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

    Drew1080

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

    ravand

    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:
    player.sendMessage(time.map.toString());
    and
    player.sendMessage(time.getHashMap().toString());

    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

    ravand

    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

    Code:java
    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:
    Code:java
    1. @EventHandler
    2. public void onPlayerJoin(PlayerJoinEvent e){
    3. Player p = e.getPlayer();
    4.  
    5. map.put(p, false);
    6. }


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

    EDIT:
    AdamQpzm
    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

    ravand

    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

    ravand

    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. :)

    http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil/7084473#7084473
     
    ravand likes this.
  14. Offline

    ravand

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

Share This Page