Solved Hashmap returning null

Discussion in 'Plugin Development' started by Mister_2, Feb 1, 2014.

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

    Mister_2

    I have a for-statement I want to do something for everyone in a hasmap. It has worked just fine until now. I did not change anything.

    Class with the for-statement:
    Code:java
    1. public class TimerR {
    2.  
    3. public static boolean tp;
    4. public MainC pl;
    5. public TimerR(MainC plugin) {
    6. pl = plugin;
    7. }
    8.  
    9. private A A;
    10. private OS OS;
    11. private V SG = new V(pl);
    12.  
    13. public void Timer() {
    14. int a = SG.getTimer() -1;
    15. Bukkit.broadcastMessage(ChatColor.BLUE + "Timer:" + a);
    16. SG.setTimer(a);
    17. for (String name : pl.Player.keySet()) {
    18. Player p = Bukkit.getPlayerExact(name);
    19. p.setLevel(SG.getTimer());
    20. if (SG.getTimer() <= 10 && tp) {
    21. p.sendMessage(SG.getPrefix() + SG.getTimer() + " seconds left!");
    22. }
    23. }
    24. if (SG.getTimer() == 0 && !tp && pl.Player.size() >= 2) {
    25. int q = 1;
    26. tp = true;
    27. SG.setTimer(11);
    28. List<String> list = A.getArenaConfig().getStringList("list");
    29. int one = pl.votes.get(list.get(0));
    30. int two = pl.votes.get(list.get(1));
    31. int three = pl.votes.get(list.get(2));
    32. int four = pl.votes.get(list.get(3));
    33. int five = pl.votes.get(list.get(4));
    34. int i = Math.max(one, Math.max(two, Math.max(three, Math.max(four, five))));
    35. for (String name : pl.votes.keySet()) {
    36. if(pl.votes.get(name) == i) {
    37. SG.setArena(name);
    38. }
    39. }
    40. for (String name : pl.Player.keySet()) {
    41. Player p = Bukkit.getPlayerExact(name);
    42. World w = Bukkit.getWorld(A.getArenaConfig().getString("spawn." + SG.getArena() + "." + q + ".world"));
    43. double x = A.getArenaConfig().getDouble("spawn." + SG.getArena() + "." + q + ".x");
    44. double y = A.getArenaConfig().getDouble("spawn." + SG.getArena() + "." + q + ".y");
    45. double z = A.getArenaConfig().getDouble("spawn." + SG.getArena() + "." + q + ".z");
    46. p.teleport(new Location(w, x, y, z));
    47. p.sendMessage(SG.getPrefix() + "Arena " + ChatColor.GREEN + SG.getArena() + ChatColor.GOLD + " has won!");
    48. p.sendMessage(SG.getPrefix() + "Teleporting you to arena...");
    49. q = q +1;
    50. }
    51. }
    52. else if (pl.Player.size() <= 2 && SG.getTimer() == 0){
    53. OS.cancel();
    54. }
    55. if (SG.getTimer() == 0 && tp && SG.getAmountOfPlayers() >= 2 && SG.getArena() != null) {
    56. OS.start();
    57. OS.chests(SG.getArena());
    58. SG.setCountDown(false);
    59. }
    60. if (SG.getArena() == null && SG.getTimer() == 0) {
    61. for (String name : pl.Player.keySet()) {
    62. Player p = Bukkit.getPlayerExact(name);
    63. p.sendMessage(SG.getPrefix() + "Arena does not exist!");
    64. p.sendMessage(SG.getPrefix() + ChatColor.BOLD + ChatColor.DARK_RED + "Teleporting everybody to Survival Games hub...");
    65. p.teleport(SG.getHub());
    66. }
    67. }
    68. else if (SG.getTimer() == 0) {
    69. for (String name : pl.Player.keySet()) {
    70. Player p = Bukkit.getPlayerExact(name);
    71. p.sendMessage(SG.getPrefix() + "Something is wrong!");
    72. p.sendMessage(SG.getPrefix() + ChatColor.BOLD + ChatColor.DARK_RED + "Teleporting everybody to Survival Games hub...");
    73. pl.Player.clear();
    74. pl.Spectator.clear();
    75. p.teleport(SG.getHub());
    76. }
    77. }
    78. if (SG.getCountDown()) {
    79. Timer();
    80. }
    81. }
    82. }


    It is this line which returns null:
    Code:java
    1. for (String name : pl.Player.keySet()) {
     
  2. Offline

    Iaccidentally

    pl is null, because you never initialized it to anything.
    On a side note, dear god please name your variables sensible things, it makes reading your code painful.
     
    Caprei likes this.
  3. Offline

    Mister_2

    Ok, I am not done at all, I will rename the variables too. But what do you mean by not initialized? I am not good with this stuff, so how would I initialize it? I thought it was initialized with
    Code:java
    1. public TimerR(MainC plugin) {
    2. pl = plugin;
    3. }
     
  4. Offline

    Iaccidentally

    whoops, must have missed that bit. In that case, pretty sure the issue is that you are calling pl.Player, which would be an invalid name. (I dont know what you have Player set as in your main class, but Bukkit already has Player)
     
  5. Offline

    Mister_2

    Oh, I didn't think it would matter. It is a Hashmap with <String, String>, I will change the name to something else.
    This really helped me alot, thank you! :)
     
Thread Status:
Not open for further replies.

Share This Page