Solved Adding Mob to List Gives StackTrace

Discussion in 'Plugin Development' started by LordVakar, Apr 16, 2014.

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

    LordVakar

    So I have this code:
    Code:
    public static List<Entity> villagerlist;
       
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args){
            if(cmd.getName().equalsIgnoreCase("guidevil")){
                Player p = (Player) sender;
                Block somewhere = getSelectedBlock(p);
                Entity npc= somewhere.getWorld().spawn(somewhere.getLocation(), Villager.class);
                ((Villager)npc).addPotionEffect(new PotionEffect(PotionEffectType.SLOW, Integer.MAX_VALUE, 128));
                ((Villager)npc).addPotionEffect(new PotionEffect(PotionEffectType.JUMP, Integer.MAX_VALUE , 6));
                villagerlist.add(npc);
            }
            return false;
        }
    And when I add the npc with villagerlist.add(npc); it gives me an error.
    How would I fix this? Is it smart to add an entity to a list since I know adding players can give you memory leaks? If you don't add an entity to a list, but something that relates to the entity like an id or something, how would I compare to see if a villager is the same entity as the npc I spawned?

    The stacktrace:

    Code:
    [08:15:52 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'guid
    evil' in plugin MinigameAPI v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:17
    5) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at org.bukkit.craftbukkit.v1_7_R1.CraftServer.dispatchCommand(CraftServe
    r.java:683) ~[craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.handleCommand(PlayerCon
    nection.java:952) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java
    :814) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.a(PacketPlayInChat.java
    :28) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInChat.handle(PacketPlayInChat
    .java:47) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146
    ) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craf
    tbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:6
    55) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:2
    50) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:5
    45) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java
    :457) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:6
    17) [craftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
    Caused by: java.lang.NullPointerException
            at me.LordVakar.MinigameAPI.Commands.CmdGuideVil.onCommand(CmdGuideVil.j
    ava:48) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[cra
    ftbukkit.jar:git-Bukkit-1.7.2-R0.3-b3020jnks]
            ... 13 more
     
  2. Offline

    Bammerbom

    @LordVakar
    Change:
    public static List<Entity> villagerlist;
    to
    public static List<Entity> villagerlist = new ArrayList<Entity>();
     
  3. Offline

    Maurdekye

    LordVakar You haven't initialized your villagerlist ArrayList. Also, you shouldn't be storing entities in an array; use their UUIDs instead. You could cause memory leaks doing that.
     
  4. Offline

    LordVakar

    Maurdekye

    As I said in the OP:
    So yes I know I could cause memory leaks.

    Jhtzb
    Maurdekye
    Thank you both, I will go edit my code now.

    EDIT: How do I add the UUID of an entity to a list?
    villagerlist.add(npc.getUniqueId());
    I get an error on add to do addAll but that still doesn't work.

    EDIT 2: Tried toString()
     
  5. Offline

    Maurdekye

    LordVakar What? That doesn't make sense; what kind of error are you getting, where and when?
     
  6. Offline

    LordVakar

    Maurdekye
    The error is:
    The method add(Entity) in the type List<Entity> is not applicable for the arguments (UUID)
    when I add the unique id to the list
    villagerlist.add(npc.getUniqueId());
     
  7. Offline

    SnipsRevival

    You need to change your list to store UUIDs or Strings (if you use the toString method).
     
  8. Offline

    LordVakar

    SnipsRevival
    So I would change my entire list to:
    public static List<UUID> villagerlist = new ArrayList<UUID>();
    ?

    And also, is this the correct import for UUID:
    import java.util.UUID;

    Also how do I define my custom mob Npc?
    Something like:
    Entity npc = _;

    In my code, I have:
    Entity npc= somewhere.getWorld().spawn(somewhere.getLocation(), Villager.class);

    What if I wanted to get "npc" from another method? How would I define npc?

    EDIT:
    Nevermind, I think I solved this.
     
Thread Status:
Not open for further replies.

Share This Page