I am trying to grab a HashMap from a different class and modify/read it in another. What i have: Main Class: Code:java if (args[0].equalsIgnoreCase("time")) {SetTime time = new SetTime(); //Constructor gives me ERROR here mapTime = time.getHashMap();player.sendMessage(mapTime.toString());return true;} SetTime Class: Code:java public class SetTime implements Listener {HashMap<Player, Boolean> map = new HashMap<Player,Boolean>(); private Main plugin;public SetTime(Main plugin){this.plugin = plugin;plugin.getServer().getPluginManager().registerEvents(this, plugin);} public HashMap<Player,Boolean> getHashMap(){return map;} 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
is the SetTime time = new SetTime(); the HashMap constructer for that class? if so try changing it to HashMap time = new HashMap();
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.
@Drew1080 So your saying that adding Code:java public SetTime(){ } would be enough? Or do i have to write anything into it?
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?
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.
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 HashMap<Player,Boolean> map = new HashMap<Player,Boolean>();map = time.getHashMap();player.sendMessage(map.get(player).toString()); Creating HashValues in SetTime class: Code:java @EventHandlerpublic void onPlayerJoin(PlayerJoinEvent e){Player p = e.getPlayer(); map.put(p, false);} 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?
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.
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
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
All good changed it to public static and accessing it in a static way and now it works thanks all for your help
ravand Exactly the way I wouldn't do it. http://stackoverflow.com/questions/7026507/why-are-static-variables-considered-evil/7084473#7084473