Error occur when add value in a Hashmap?

Discussion in 'Plugin Development' started by loman, Apr 21, 2020.

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

    loman

    Hello, I am making a system which will initially put 8 into the hashmap if the key is not found and every second will minus 1 of the hashmap.

    Code:Java
    1. @EventHandler
    2. public void onrightclick(PlayerInteractEvent event) {
    3. if (event.getAction().equals(Action.RIGHT_CLICK_AIR) || event.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    4. if (event.getPlayer().getInventory().getItemInMainHand().getItemMeta().getLore().toString().contains("aaa")) {
    5. Player player = event.getPlayer();
    6. if (!this.cooldown.containsKey(player)) {
    7. this.cooldown.put(player, 8);
    8. getLogger().info(cooldown.get(player).toString());
    9. player.addPotionEffect(new PotionEffect(PotionEffectType.GLOWING, 160, 1));
    10. for(int i=0; i <8; i++){
    11. Bukkit.getScheduler().scheduleSyncDelayedTask(this, new Runnable() {
    12. @Override
    13. public void run() {
    14. int timeleft = cooldown.get(player)-1;
    15. cooldown.put(player,timeleft);
    16. }
    17. }, 20L);
    18. }this.cooldown.remove(player);
    19. }
    20. }
    21. }
    22. }


    However, when I try to run this on my server, it shows this error:

    Code:
    [11:31:12 WARN]: [bounce] Task #8 for bounce v1.0 generated an exception
    java.lang.NullPointerException: null
            at com.blackhat.bounce.Main$1.run(Main.java:45) ~[?:?]
            at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftTask.run(CraftTask.java:76) ~[spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at org.bukkit.craftbukkit.v1_12_R1.scheduler.CraftScheduler.mainThreadHeartbeat(CraftScheduler.java:361) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.D(MinecraftServer.java:739) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.DedicatedServer.D(DedicatedServer.java:406) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.C(MinecraftServer.java:679) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at net.minecraft.server.v1_12_R1.MinecraftServer.run(MinecraftServer.java:577) [spigot-1.12.2.jar:git-Spigot-dcd1643-e60fc34]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_241]
    and line 45 is:
    int timeleft = cooldown.get(player)-1; (which is inside the Bukkit.getScheduler().scheduleSyncDelayedTask)

    can anyone tell me what problem is in my plugin so that the error occurs?
    thx so much!
     
  2. Offline

    RBT_KitK4t

    Can you please send more of your code (like the HashMap and where/when it is instantiated) ? Otherwise, it is hard to figure whether or not your HashMap is really created..

    Sent from my SM-T580 using Tapatalk
     
  3. Offline

    Strahan

    You're removing the player from the map after setting up the runnable. By the time the runnable fires off, the player isn't in the map anymore.

    What are you trying to do? Just a basic cooldown? If so, that is a very awkward way to do it. Just make a Map<UUID, Long> and when the person performs an action subject to cooldown, check if they are in the map. If they are, check if the map value is >= currentTimeMillis(). If not, tell them it's too early. If they weren't in the map, then you just do Map.put(playerObject.getUniqueId(), System.currentTimeMillis()+msCooldownDuration).
     
  4. Offline

    loman

    @Strahan
    Oh! What a great idea!
    Thank you for your suggestion
    I will try it out
     
Thread Status:
Not open for further replies.

Share This Page