Solved HashMap returning null

Discussion in 'Plugin Development' started by Tomasko, Mar 6, 2014.

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

    Tomasko

    Hello, if you can please help me, thanks :)
    In event is hashMap returning null, but in "setting" are values good :/
    Code:java
    1. /*
    2.  
    3.  
    4. package supermob;
    5.  
    6. import java.util.HashMap;
    7. import org.bukkit.Bukkit;
    8. import org.bukkit.event.EventHandler;
    9. import org.bukkit.event.Listener;
    10. import org.bukkit.event.entity.EntityDamageEvent;
    11.  
    12. /**
    13. *
    14. * @author Tomas
    15. */
    16. public class MoreHealth implements Listener {
    17. public HashMap<String, Double> list = new HashMap<String, Double>();
    18.  
    19.  
    20. @EventHandler
    21. public void onDamage(EntityDamageEvent e) {
    22. Bukkit.getServer().broadcastMessage("hit");
    23.  
    24.  
    25. String nameid = String.valueOf(e.getEntity().getEntityId());
    26. Bukkit.getServer().broadcastMessage(String.valueOf(list.get(nameid))); //unformantely, return null
    27. if(list.containsKey(nameid)) {
    28. if(list.get(nameid) > 20) {
    29. double health = list.get(nameid);
    30. Bukkit.getServer().broadcastMessage(String.valueOf(health));
    31. if((list.get(nameid) - e.getDamage()) > 20) {
    32. Bukkit.getServer().broadcastMessage("no damage");
    33. list.put(nameid, health - e.getDamage());
    34. e.setCancelled(true);
    35. } else {
    36. Bukkit.getServer().broadcastMessage("damage");
    37. if(list.containsKey(nameid)) {
    38. list.remove(nameid);
    39. }
    40. }
    41. }
    42. }
    43. }
    44.  
    45. public void setHealt(int EntityID, double health) {
    46. String nameid = String.valueOf(EntityID);
    47. list.put(nameid, health);
    48. Bukkit.getServer().broadcastMessage(String.valueOf(list.get(nameid))); //return good value
    49.  
    50. }
    51.  
    52. public double getHealt(int EntityID) {
    53. String nameid = String.valueOf(EntityID);
    54. Bukkit.getServer().broadcastMessage(String.valueOf(list.get(nameid))); //return good value
    55. if(list.containsKey(nameid)) {
    56. return list.get(nameid);
    57. }
    58. return -1;
    59. }
    60.  
    61. }
    62.  
     
  2. Offline

    Alshain01

    Your not putting anything in it. You declared it as a new HashMap and they start empty when you do that. Your other two attempt to access it call list.put() first so of course they aren't going to error (though putting it in the map just to get it back is a bit redundant).
     
  3. Offline

    Tomasko

    Okey, and noobish question, how can I put value into hashmap(in this code) ?
     
  4. Offline

    nlthijs48

    If no entry for the Key string exists it returns null and not 0 as you might expect, so just check if it is null before doing something with it.
     
  5. Offline

    Tomasko

    Thanks
     
Thread Status:
Not open for further replies.

Share This Page