Solved Why is this throwing a Null pointer?

Discussion in 'Plugin Development' started by stanleymanley123, Aug 17, 2015.

Thread Status:
Not open for further replies.
  1. Code:
    private HashMap<String, Integer> numbers = new HashMap<String, Integer>();
        private HashMap<String, Integer> taskIDS = new HashMap<String, Integer>();
      
        public void show(final Player p, final ItemStack winning) {
            p.openInventory(inv);
            players.add(p.getName());
            setWinningItem(p, winning);
            numbers.put(p.getName(), 0);
            taskIDS.put(p.getName(), Bukkit.getScheduler().scheduleSyncRepeatingTask(Main.getPlugin(), new Runnable() {
                public void run() {
                    if(numbers.get(p.getName()) != 20) {
                        numbers.put(p.getName(), numbers.get(p.getName()) + 1);
                      
                        Random random = new Random();
                        ItemStack item = items.get(random.nextInt(items.size()));
                      
                        inv.setItem(4, item);
                        p.getWorld().playSound(p.getLocation(), Sound.CLICK, 1.0F, 2.0F);
                    }else{
                        Bukkit.getScheduler().cancelTask(taskIDS.get(p.getName()));
                        taskIDS.remove(p.getName());
                        numbers.remove(p.getName());
                      
                        //players.remove(p.getName());
                        players.remove(p.getName());
                      
                        inv.setItem(4, winning);
                        p.getWorld().playSound(p.getLocation(), Sound.LEVEL_UP, 1.0F, 1.0F);
                      
                        Bukkit.broadcastMessage(ChatColor.GREEN + p.getName() + " unboxed a " + ChatColor.AQUA + "STATIC CRATE " + ChatColor.GREEN + "and got a " + winning.getItemMeta().getDisplayName());
                    }
                }
            }, 0, 5));
        }
      
        public void setWinningItem(Player p, ItemStack item) {
            CrateManager.getCrateManager().getConfig().set("winning.item." + p.getName(), item);
        }
      
        public ItemStack getWinningItem(Player p) {
            return CrateManager.getConfig().getItemStack("winning.item." + p.getName());
        }
      
        @EventHandler
        public void onInventoryClose(InventoryCloseEvent e) {
            Player p = (Player)e.getPlayer();
            if(!e.getInventory().getName().equalsIgnoreCase(inv.getName())) return;
            if(!players.contains(p.getName())) return;
            p.getInventory().addItem(getWinningItem(p));
            CrateManager.getCrateManager().getConfig().set("winning.item." + p.getName(), null);
            if(players.contains(p.getName())) {
            Bukkit.getScheduler().cancelTask(taskIDS.get(p.getName()));
            p.sendMessage("hi");
            }
            taskIDS.remove(p.getName());
            numbers.remove(p.getName());
            players.remove(p.getName());
        }
    I'm getting an NPE on the line "taskIDS.remove(p.getName());" (Line 55) in the InventoryCloseEvent, yet you can clearly see that I put the player's name back in show method. Why is this?
     
  2. Offline

    caderape

    @stanleymanley123
    Just check if it's not null. You also remove it with the scheduler afte 5 seconds.
     
  3. Might I ask, what should I check if it's not null?
    Code:
    if(!taskIDS.get(p.getName() != null) {
    taskIDS.remove(p.getName());
    }
    Would this be correct?

    EDIT: If I do that, the taskID would never be cancelled as taskIDS.get(p.getName()) would return null, and I know that the taskID is not not cancelled because as you can see in my code every 5 ticks I do a click sound, and after I close the inventory the click sound still continues, making me assume that the taskID is still going.
     
  4. Offline

    Synapz

    @stanleymanley123
    Check if the player is null. If the player is null the player isn't online and should be referenced as an OfflinePlayer (do that only if you need the player otherwise just say "Player not found").

    That code can be used just check if the player is null-- you just have to edit it a bit which I'm sure you know what to do, your just checking if the player is null. If they are null (they aren't online) meaning you can either make them an offline player and add them. If you choose this approach you should use a more abstract List to handle your taskID so you can add both OfflinePlayer and Player... Or just send the sender of the command a message saying the player isn't online.

    P.S - I assume its the player that is null. If this doesn't work then I will recheck to see if I can come up with something else
     
  5. Fixed. My Hashmaps were not static, so I added static and it fixed everything.
    Thanks for the help you two.
    @Synapz @caderape
     
    Synapz likes this.
  6. Offline

    lubbs31

    Set as "solved"
     
Thread Status:
Not open for further replies.

Share This Page