Solved NullPointer exception

Discussion in 'Spigot Plugin Development' started by SimPlaysGames, May 12, 2016.

  1. Hey, I am getting a nullpointer exception can you please help me fix it

    code:
    http://pastebin.com/9nj9X2Wt

    Code (Text):
    [16:31:55 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'remove' in plugin CommandMenu v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_65]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_65]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_65]
    Caused by: java.lang.NullPointerException
            at me.simplaysgames.commandmenu.commands.Remove.getEntity(Remove.java:73) ~[?:?]
            at me.simplaysgames.commandmenu.commands.Remove.onCommand(Remove.java:21) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            ... 15 more
    I also got this error code when I tried one the one with args

    Code (Text):
    [16:36:01 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'remove' in plugin CommandMenu v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_65]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_65]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_65]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 0
            at me.simplaysgames.commandmenu.commands.Remove.onCommand(Remove.java:29) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-e4d4710-e1ebe52]
            ... 15 more
     
  2. You need to check if the entity has a custom name before you try to get it.

    For the second one, you only need to do something like this:
    Code (Text):
    int whateveryouwanttocallit = Integer.parseInt(args[0]);
    No need to make another array.
     
  3. Also there is a fancy method called getNearbyEntities()
    It would be easier if you just used that instead of getting all the entities in the world and checking if they were near you with distance().
    It could potentially be more efficient though if you used distanceSquared() (and squared the distance number) because distance uses the sqrt function which is much harder on the computer.
     
  4. Code (Text):
    for(Entity ent : player.getNearbyEntities(5,5,5){
    if(!(ent.hasCustomName()){
    return;
    }
    if(ent.getCustomName().equalsIgnoreCase("ENTNAME"){
    // Coderino
    }
     
  5. Man what is it with people spoonfeeding today. <,<
     
  6. My bad, I forgot to say it was 1.8.8, so that wont work

    I didn't need this, and when you spoonfeed please add comments thanks^-^
     
  7. What do you mean that wont work?
    I gave you two options, one that was easy and one that was more efficient. >.> The second one is just changing a little bit of the code you already have so it doesn't use the sqrt function...
     
  8. It's hardly spoonfeeding. You can find it literally by typing in "how to get nearby entities in minecraft coding". Simple as pie, not too hard to figure it out.
     
  9. Ok, but for 1.8 the hasCustom name thing doesn't work, would I do

    e.getCustomName() == null
     
  10. try if statements
     
  11. Yes. if their name == null, it's the same as has.
     
  12. I know that, but I just wanted to know the replacement for e.hasCustomName
     
  13. != means not equals to
    == means is equals to

    well then use the if statement to check if the player custom name is not equals to null
     
  14. I think what the OP is trying to do is return if the name is null.
     
  15. Or check if it IS null, then return.
     
  16. Guys, thanks for the help. I got it working!
     
    • Friendly Friendly x 1