Getting random: Generated an Exception (NPE)

Discussion in 'Plugin Development' started by kayc01, Nov 7, 2015.

Thread Status:
Not open for further replies.
  1. Hey, so i recently made a queue system for my plugin however for some reason it seems to be getting a NPE, but not giving any error that tells me where the NPE is.

    So i have no idea where to correct it.
    You guys may know though..


    Code:
    [00:37:36] [Server thread/WARN]: [PixelmonGym] Task #248199 for PixelmonGym v5.0 generated an exception
    java.lang.NullPointerException
    [00:37:36] [Server thread/WARN]: [PixelmonGym] Task #133194 for PixelmonGym v5.0 generated an exception
    java.lang.NullPointerException
    Is all it spams.

    Is it that if no one is in those list's that it is literally spamming that it is null?
    But shouldn't it still point me to which part of the code is null?

    I am quite confused by this.

    Here is the queue code:


    Code:
    if ((args[0].equalsIgnoreCase("winner") || args[0].equalsIgnoreCase("win")  || args[0].equalsIgnoreCase("w"))) {
                              
                                 for (Player playerWinner : Bukkit.getOnlinePlayers()) {
    
                               
                                 int gym;
                                  
                               
                                  String gymArg = args[1].replace("gym", "");
                                
                                  try {
    
                                      // Set the gym variable to the number in arguement 1
    
                                      gym = Integer.parseInt(gymArg);
    
                                  } catch (NumberFormatException nfe) {
    
                                      p.sendMessage(ChatColor.RED + args[1] + " is not a gym!");
    
                                      // Number was not a number
    
                                      return true;
    
                                  }
    
                                  if (gym < 1 || gym > 32) {
    
                                      // Number was not between 1 and 32
    
                                      return true;
    
                                  }
                               
                                  int u = gym +1;
                                
                                 if (playerWinner.getName().equalsIgnoreCase(args[2])) {
                                if (p.hasPermission("pixelgym."+args[1]) || p.hasPermission("pixelgym.admin")) {
                                  
                                    final Player o = (Player) playerWinner;
                                  
                                    if (queues.get(gym).contains(o.getUniqueId())) {  
                                      //UUID uuid = p.getUniqueId();
                                  
                                      //UUID uuid = queues.get(gym).get(0);
                                      //Player playerToTeleport = Bukkit.getPlayer(uuid);
                                if ((settings.getData().get("warps.spawn") != null)) {
                                    World w = Bukkit.getServer().getWorld(settings.getData().getString("warps.spawn.world"));
                                    double x = settings.getData().getDouble("warps.spawn.x");
                                    double y = settings.getData().getDouble("warps.spawn.y");
                                    double z = settings.getData().getDouble("warps.spawn.z");
                                    playerWinner.teleport(new Location(w, x, y, z));
                                    playerWinner.sendMessage(ChatColor.GREEN + "Teleported out of " + ChatColor.YELLOW + ChatColor.BOLD + "gym"+gym + "!");
                                    playerWinner.sendMessage(ChatColor.GREEN + "Congrats, you won the gym"+gym + " badge! (" + ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym"+gym+"colour")+ ChatColor.BOLD + getConfig().getString("config.gym"+gym)+ChatColor.GREEN+ ") " + "Now you can join the gym"+u+ " queue with /gym join gym"+u+". ("+ChatColor.translateAlternateColorCodes('&', getConfig().getString("config.gym"+u+"colour")+ ChatColor.BOLD + getConfig().getString("config.gym"+u)+ ChatColor.GREEN + ")")));
                                  
                                    p.sendMessage(ChatColor.GREEN + "successfully Telported " +playerWinner.getName() + " out of your gym!");
                                    p.sendMessage(ChatColor.GREEN + "You are now ready for your next battle, type: /gym next gym"+gym);
                                }
                                else {
                                    p.sendMessage(ChatColor.RED + "Warp point 'spawn' does not exist. Type: /gym setwarp spawn. (this is the teleport location to move challengers out of the gym.");
                                }
    
                                    final String gymArg2 = args[1].replace("gym", "");
                                    final int gym2 = Integer.parseInt(gymArg2);
                                  
                                  
                             
                                    queues.get(gym).remove(0);
                                    cooldownTime.put(o, 60);
                                    cooldownGym.get(gym).add(o);
                                    cooldownTask.put(o, new BukkitRunnable() {
                                      
                                        @Override
                                        public void run() {
    
                                            cooldownTime.put(o, cooldownTime.get(o) - 1);
                                            if (cooldownTime.get(o) == 0) {
                                                cooldownTime.remove(o);
                                                cooldownTask.remove(o);
                                                PixelGym.get().cooldownGym.get(gym2).remove(o);
                                                cancel();
                                            }
                                        }
                                      
                                    });
                                  
                                    cooldownTask.get(o).runTaskTimer(this, 20, 1200);
                                  
                                   //cooldowns.remove(uuid)
                                          
                             
                             
                                    for (int i = 1; i <= 32; i++) {
                                     if (args[1].equalsIgnoreCase("gym"+i)) {
                                         settings.getBadge().set("Players." + playerWinner.getName() + ".Badges." +args[1], "Won");
                                         settings.saveBadges();
                                     }
                                    }
                                         Bukkit.broadcastMessage(ChatColor.DARK_GRAY + "[" + ChatColor.AQUA + getConfig().getString("config.title") + ChatColor.DARK_GRAY + "] " + ChatColor.YELLOW + playerWinner.getName() + ChatColor.translateAlternateColorCodes('&', getConfig().getString("config."+args[1]+"colour")) +  " has won the " + getConfig().getString("config."+args[1]) + " Gym Badge!");
                                         //settings.getBadge().set("Players." + playerWinner.getName() + ".Badges", playerWinner.getInventory().getContents());
                                       
                                    }
                                       else {
                                           p.sendMessage(ChatColor.RED + "Player must be in the queue to win or lose!");
                                       }
                                }
                                else {
                                    p.sendMessage(ChatColor.RED + "You do not have permission to set winner's of "+args[1]);
                                  
                                        }
                                     }
                                 }
                           
                             }
    Thanks!

    EDIT:
    Could it possibly be that if a player goes offline that is in the cooldown list, and the cooldown is still updating that it will generate the NPE?
    If that's possibly the case, how could i stop the cooldown for that player whilst they are offline? Then continue when they come back online? I don't want it to remove them because it will become a way to get around the cooldown system.
     
    Last edited: Nov 7, 2015
  2. Offline

    adam753

    My guess is the exception is coming form your BukkitRunnable task. The API caught the exception and is telling you about it. Shouldn't be hard to figure out if something is null in there, maybe cooldownTime.get(o) is returning null under some condition?

    Well, I can't see what data types you have here, but you're correct that you should never rely on a Player object to still exist outside the immediate scope (like adding it to a list or hashmap, etc.) Use the Player's UUID instead.
     
  3. Ok ill try convert it to UUID, however in the mean time also when a player goes offline it does re-set the cooldown (removes it) any way i can prevent that?

    EDIT: Nvm the UUID seems to solve that.
    I guess it can store the UUID when the player goes offline unlike playerName can't.
    :3

    Sent to server for testing on the NPE.
    I will post if it still occurs.
     
    Last edited: Nov 7, 2015
  4. Offline

    adam753

    @kayc01
    You would find out the NPE line if you catch it and print it yourself

    Code:java
    1. cooldownTask.put(o, new BukkitRunnable() {
    2. @Override
    3. public void run() {
    4.  
    5. try {
    6.  
    7. cooldownTime.put(o, cooldownTime.get(o) - 1);
    8. if (cooldownTime.get(o) == 0) {
    9. cooldownTime.remove(o);
    10. cooldownTask.remove(o);
    11. PixelGym.get().cooldownGym.get(gym2).remove(o);
    12. cancel();
    13. }
    14. }
    15.  
    16. }
    17. e.printStackTrace();
    18. }
    19.  
    20. }
    21. });
     
Thread Status:
Not open for further replies.

Share This Page