Possible nullpointer or may not contain object of type material

Discussion in 'Spigot Plugin Development' started by Scorpion, Jun 28, 2016.

  1. Code (Text):
        @EventHandler
        public void onPlayerDeath(PlayerDeathEvent e) {
            Player p = e.getEntity();

            List<ItemStack> inv = e.getDrops();

            for (int i = 0; i < 38; i++) {
                Material m = Material.PAPER;

                for (int n = 0; n < inv.size(); n++) {
                    ItemStack itm = inv.get(n);
                    if (itm != null) {
                        if (itm.getType().equals(m)) {
                            if (itm.getItemMeta().getDisplayName().equalsIgnoreCase(plugin.getConfig().getString("ClueSettings.name"))) {
                                inv.remove(m);
                            }
                        }
                    }
                }
                plugin.remove(p);
            }
        }
    There is no test errors yet merely the program giving loads of possible nullpointers which can often be fixed by simply changing something.

    In my time scripting i've noticed that grabbing someones inventory on death is almost always empty due to the drops pretty much already being on the floor by the time checks are performed.
    So i tried using getdrops and scan through them.

    Testing hasn't yet been done but on the last section where it checks if the item is similar to a name in the config and then performs "inv.remove(m);" the m is yellow suggesting that it may not contain object of type: material

    So that could cause issues in the future, How can i best get around that possible error.
     
  2. Your List only contains ItemStacks, but you are trying to remove a Material. I think you wanted to inv.remove(n) instead of m.