Solved Cooldown, NullPointerException

Discussion in 'Plugin Development' started by ColaCraft, Aug 9, 2013.

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

    ColaCraft

    Essentially, when I try to use my code, I get this exception every time:

    [​IMG]

    Here are the snips of code you'll need to debunk this- I am honestly not sure how to use the timer, as I haven't worked with them before. Here is the tutorial I followed: https://forums.bukkit.org/threads/tutorial-simple-cooldown-timer-newb-friendly.156283/

    This is the Cooldown.java class (Cooldown.java):

    Code:java
    1. package me.PorterK.RPG;
    2. // Import your goodies
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.entity.Player;
    5. import org.bukkit.scheduler.BukkitRunnable;
    6.  
    7. //Extend BukkitRunnable
    8. public class Cooldown extends BukkitRunnable {
    9. // Constructor to fetch variables
    10. public main plugin;
    11.  
    12. public Cooldown(main plugin){
    13. this.plugin = plugin;
    14. }
    15. // This is what happens when you call in this task
    16. public void run() {
    17. //for every person online
    18. for(Player player : Bukkit.getServer().getOnlinePlayers()) {
    19. // Set a cooldown hashmap for the player down by one
    20. plugin.cooldown1.put(player.getName(), plugin.cooldown1.get(player.getName()) - 1);
    21. }
    22. }
    23. }


    Next would be the onEnable for my main class (main.java):

    Code:java
    1. /**
    2. *
    3. */
    4. package me.PorterK.RPG;
    5.  
    6. import java.util.ArrayList;
    7. import java.util.HashMap;
    8. import java.util.List;
    9.  
    10. import org.bukkit.ChatColor;
    11. import org.bukkit.enchantments.Enchantment;
    12. import org.bukkit.inventory.meta.ItemMeta;
    13. import org.bukkit.plugin.java.JavaPlugin;
    14. import org.bukkit.scheduler.BukkitTask;
    15.  
    16.  
    17. public class main extends JavaPlugin{
    18.  
    19. public Cooldown cooldown;
    20. HashMap<String, Integer> cooldown1;
    21. // create as many "cooldown1", 2, 3, 4, etc as you need.
    22. // Just remember to add them into the cooldown class.
    23. @Override
    24. public void onEnable(){
    25. // All of your enable stuff goes here.
    26. // The run method from your cooldown class will be run every 20 ticks, AKA 1 second
    27. BukkitTask Cooldown = new Cooldown(this).runTaskTimer(this, 20, 20);
    28. // Creates a new hashmap, for a player and the number of seconds left in the cooldown.
    29. cooldown1 = new HashMap<String, Integer>();
    30. }


    Finally, here is where I try to utilize the code (ClassListener.java):

    Code:java
    1. if((event.getAction().equals(Action.LEFT_CLICK_AIR) || event.getAction().equals(Action.LEFT_CLICK_BLOCK)) && event.getPlayer().getInventory().getItemInHand().getType().equals(Material.STICK)){
    2.  
    3. if(plugin.getConfig().getString("users." + name + ".class").equalsIgnoreCase("Wizard")){
    4.  
    5. // Remember to have a constructor for the main class where this hashmap is stored.
    6. // This below fetches the amount of seconds left and checks if its lower than 0.
    7. if (plugin.cooldown1.get(p.getName()) <= 0) {
    8. // This will then set the cooldown to 10 seconds
    9. plugin.cooldown1.put(p.getName(), 3);
    10.  
    11. Fireball fb = (Fireball) p.getWorld().spawn(p.getEyeLocation(), Fireball.class);
    12. fb.setShooter(p);
    13. fb.setVelocity(p.getLocation().getDirection().multiply(1.0));
    14.  
    15. }
    16. else {
    17. // Cooldown notifier.
    18. p.sendMessage(ChatColor.RED + "You must wait " + ChatColor.BLUE + plugin.cooldown1.get(p.getName()) + ChatColor.RED + " seconds.");
    19. }
    20.  
    21.  
    22. }
    23. }



    Thanks for any help you might have to offer in helping me figure this out.
     
  2. Offline

    Xx_LeetGamer_xX

    Your problem is this:

    Code:
    plugin.cooldown1.put(player.getName(), plugin.cooldown1.get(player.getName()) - 1);
    
    "plugin.cooldown1.get(player.getName())" is returning null because its null by default and its not being set to anything else. onPlayerJoinEvent, set that player's cool down to zero if it is null.
     
  3. Offline

    ColaCraft

    I've added this in to my listener class, & I am still getting the same error:

    Code:java
    1. public void onPlayerJoinEvent(PlayerJoinEvent event){
    2. Player p = event.getPlayer();
    3. String name = p.getName();
    4.  
    5.  
    6. if(!plugin.cooldown1.containsKey(name)){
    7.  
    8. plugin.cooldown1.put(name, 0);
    9. }
    10. }


    Xx_LeetGamer_xX
     
  4. Offline

    Xx_LeetGamer_xX

    Maybe this then, it seems like it should do the same thing but this is the exact syntax I use in my projects:

    Code:
    if(plugin.cooldown1.get(name) == null){
        plugin.cooldown1.put(name, 0);
    }
    
     
  5. Offline

    ColaCraft


    No avail. Could it be that in my code I use:

    Code:java
    1. plugin.cooldown1.put(player.getName(), plugin.cooldown1.get(player.getName()) - 1);


    but in the tutorial code it does this:

    Code:java
    1. plugin.cooldown1.put(player.getName(), plugin.cooldown2.get(player.getName()) - 1);


    The only issue with the tutorial code is the fact that "cooldown2" isn't defined anywhere.
     
  6. Offline

    Xx_LeetGamer_xX

    If cooldown2 isn't being defined anywhere then what you put (cooldown1) is correct.

    Maybe this, might not be the best solution, but it should work:

    Code:
    for(Player player : Bukkit.getServer().getOnlinePlayers()) {
        // Set a cooldown hashmap for the player down by one
        try{
            plugin.cooldown1.put(player.getName(), plugin.cooldown1.get(player.getName()) - 1);
        }catch(Exception e){
            plugin.cooldown1.put(player.getName(), 0);
        }
    }
    
     
    ColaCraft likes this.
  7. Offline

    ColaCraft

Thread Status:
Not open for further replies.

Share This Page