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(PluginCommand.java:46) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_131]
    at java.util.concurrent.FutureTask.run(Unknown 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(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_131]
    Caused by: java.lang.NullPointerException
    at commands.FreezeCommand.onCommand(FreezeCommand.java:38) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
    ... 15 more
    [12:17:37]

    I think the problem is in the structure of the code itself, so I've included a picture below.
    http://i.imgur.com/G2KglZf.png
    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: https://docs.oracle.com/javase/tutorial/java/concepts/index.html
     
    • Like Like x 1