OnDisable null point error

Discussion in 'Spigot Plugin Development' started by MrAxeTv, Apr 21, 2017.

  1. Hello there guys I need some help I am getting null point error from my array list
    Code (Text):

    import java.util.ArrayList;
    import java.util.UUID;

    import org.bukkit.Bukkit;
    import org.bukkit.World;
    import org.bukkit.entity.Entity;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.CreatureSpawnEvent;
    import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.EntityDeathEvent;

    import me.KikiCro26.BeastSpawnerLevelUp.Main;

    public class MobDeath implements Listener {
        private Main  pl;
        public ArrayList<UUID> list;
        public MobDeath(Main plugin){
            pl = plugin;
            list = new ArrayList<UUID>();
        public void onCreatureSpawn(CreatureSpawnEvent e){

        public void killMob(EntityDeathEvent e){

        public void damage(EntityDamageByEntityEvent e){
        public void removeMobs(){
            for (World world : Bukkit.getWorlds()) {
                for (Entity e : world.getEntities()) {

    Problem is only in removeMobs Method
    I call that method in oDdisable and look like arraylist is already empty
    because when I try to print out first index of list there is nothing I get null poin error
    any suggestion
  2. Even thn send us the stactrace ill see into it when i reach home
  3. You should
    • Use Sets rather than Lists
      • Aside that, just calling Collection::remove(Object} is enough, since it returns true when the Collection contained the element (i.e. no need to both call contains and remove).
      • Moreover, since Sets don't allow duplicates, no need to check contains before add.
    • Use abstractions rather than implementations as field types (List rather than ArrayList)
    • Make your fields private.
    There's no real reason for the method to throw a NPE, unless you set the List to null manually in another class (which is why you don't make it public)
  4. At first, you dont need to check if the list contains the object before removing the object; just use list.remove(o). That's enough.
    And why are you iterating the mobs of the world? You could just iterate your ArrayList and remove the objects. You could also use an Array, a list is a bit useless.

    Furthermore i recommend to add a meta tag to the entity. You could archive that with setmetadata. Therefore the remove method would also work after a reload or restart.
  5. Arrays don't grow in size, that's why
    Bukkit metadata isn't retained across restarts
  6. Interesting, i don't knew that.
    In that case you have to use the NBT-Tag of the entity and add a key for your custom data..
  7. I typically just remove all custom mobs or anything created or tracked in a list on disable. Typically if I am tracking an entity and it is worthy of adding to a list or variable I want my plugin to fully handle it including its removal.

    But yes we need to see a stack trace to be able to help you find what's causing the error

    Sent from my iPhone using Tapatalk