Solved Null pointer exception when target not found

Discussion in 'Spigot Plugin Development' started by black_, Apr 25, 2017.

  1. When I cast a command, in this case freezing a player, and the player is not on my server, a get a null pointer exception. However, I thought I fixed this by checking if the target is null, and if it is returning.
    Here the code (for that section only)
    Code (Text):

    Player target = Bukkit.getServer().getPlayer(args[0]);
    if (target == null) {
        p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("playernotfoundmsg").replace("%target%", target.getName())));
        return true;
    Here is the stack trace:

    [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'ss' in plugin Freeze v1.0
    at org.bukkit.command.PluginCommand.execute( ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.command.SimpleCommandMap.dispatch( ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand( ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand( [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a( [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a( [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a( [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$ [spigot.jar:git-Spigot-db6de12-18fbb24]
    at java.util.concurrent.Executors$ Source) [?:1.8.0_131]
    at Source) [?:1.8.0_131]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B( [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B( [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A( [spigot.jar:git-Spigot-db6de12-18fbb24]
    at [spigot.jar:git-Spigot-db6de12-18fbb24]
    at Source) [?:1.8.0_131]
    Caused by: java.lang.NullPointerException
    at commands.FreezeCommand.onCommand( ~[?:?]
    at org.bukkit.command.PluginCommand.execute( ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    ... 15 more

    I think the problem is in the structure of the code itself, so I've included a picture below.
    Ask if you need to see other classes.
    Thanks for your help!
  2. If target is null, you calling target.getName() will obviously throw an NPE. Since the name equals args[0], use that.

    Edit: you might want to learn about oop.
  3. You are completely right. I had args[0] there before, but when I changed the message to make it more configurable, I forgot about that. Thanks so much.

    Regarding OOP, do you have any resources that might be useful?

  4. I found this tutorial very useful:
    • Like Like x 1