Why would this be throwing a NPE

Discussion in 'Spigot Plugin Development' started by Redrield, Dec 31, 2015.

  1. I just updated one of my plugins, and now every time the main command is run, it works, but it tells them an internal error occured, and it spews out this stack trace:
    Code (Text):
    31.12 00:16:55 [Server] INFO ... 15 more
    31.12 00:16:55 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.8.8.jar:git-Spigot-5f38d38-18fbb24]
    31.12 00:16:55 [Server] INFO at BountyCommands.onCommand(BountyCommands.java:133) ~[?:?]
    31.12 00:16:55 [Server] INFO at BountiesMain.replaceConfigPlaceholders(BountiesMain.java:143) ~[?:?]
    31.12 00:16:55 [Server] INFO Caused by: java.lang.NullPointerException
    31.12 00:16:55 [Server] INFO at java.lang.Thread.run(Thread.java:745) [?:1.8.0_60]
    31.12 00:16:55 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot-1.8.8.jar:git-Spigot-5f38d38-18fbb24]
    31.12 00:16:55 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot-1.8.8.jar:git-Spigot-5f38d38-18fbb24]
    31.12 00:16:55 [Server] INFO at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot-1.8.8.jar:git-Spigot-5f38d38-18fbb24]
    31.12 00:16:55 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot-1.8.8.jar:git-Spigot-5f38d38-18fbb24]
    31.12 00:16:55 [Server] INFO at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot-1.8.8.jar:git-Spigot-5f38d38-18fbb24]
    31.12 00:16:55 [Server] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_60]
    31.12 00:16:55 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_60]
    31.12 00:16:55 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.8.8.jar:git-Spigot-5f38d38-18fbb24]
    31.12 00:16:55 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.8.8.jar:git-Spigot-5f38d38-18fbb24]
    31.12 00:16:55 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.8.8.jar:git-Spigot-5f38d38-18fbb24]
    31.12 00:16:55 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [spigot-1.8.8.jar:git-Spigot-5f38d38-18fbb24]
    31.12 00:16:55 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [spigot-1.8.8.jar:git-Spigot-5f38d38-18fbb24]
    31.12 00:16:55 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[spigot-1.8.8.jar:git-Spigot-5f38d38-18fbb24]
    31.12 00:16:55 [Server] INFO at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.8.8.jar:git-Spigot-5f38d38-18fbb24]
    31.12 00:16:55 [Server] INFO at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.8.8.jar:git-Spigot-5f38d38-18fbb24]
    31.12 00:16:55 [Server] INFO org.bukkit.command.CommandException: Unhandled exception executing command 'bounty' in plugin BountiesPlus v1.0
    31.12 00:16:55 [Server] ERROR null
    This is the code it is pointing to:
    Code (Text):

            String replacePlaceholders = str.replaceAll("%GREEN%", ChatColor.GREEN + "").replaceAll("%DARK_GREEN%", ChatColor.DARK_GREEN + "")
                    .replaceAll("%GOLD%", ChatColor.GOLD + "").replaceAll("%AQUA%", ChatColor.AQUA + "").replaceAll("%BLACK%", ChatColor.BLACK + "")
                    .replaceAll("%BLUE%", ChatColor.BLUE + "").replaceAll("%DARK_BLUE%", ChatColor.DARK_BLUE + "").replaceAll("%BOLD%", ChatColor.BOLD + "").replaceAll("%DARK_AQUA%", ChatColor.DARK_AQUA + "")
                    .replaceAll("%DARK_GRAY%", ChatColor.DARK_GRAY + "").replaceAll("%DARK_PURPLE%", ChatColor.DARK_PURPLE + "").replaceAll("%DARK_RED%", ChatColor.DARK_RED + "").replaceAll("%RED%", ChatColor.RED + "")
                    .replaceAll("%GRAY%", ChatColor.GRAY + "").replaceAll("%ITALIC%", ChatColor.ITALIC + "").replaceAll("%LIGHT_PURPLE%", ChatColor.LIGHT_PURPLE + "").replaceAll("%MAGIC%", ChatColor.MAGIC + "").replaceAll("%STRIKETHROUGH%", ChatColor.STRIKETHROUGH + "")
                    .replaceAll("%UNDERLINE%", ChatColor.UNDERLINE + "").replaceAll("%WHITE%", ChatColor.WHITE + "").replaceAll("%YELLOW%", ChatColor.YELLOW + "").replaceAll("%RESET%", ChatColor.RESET + "");
    And I cant think of any reason why it would be throwing an NPE, any help is appreciated, thanks
     
  2. str is null. Also, use .replace. .replaceAll is for regex. Lastly, you could simplify that much more to
    Code (Java):
    for(ChatColor chatColor : ChatColor.values()) {
        str = str.replace("%" + chatColor.name() + "%", chatColor.toString());
    }
     
    • Like Like x 1
  3. I replaced the code I had before with that, and it's still throwing the exception. This time at the start of the for loop, at for(ChatColor cc : ChatColor.values())
     
  4. That is literally impossible. It would be nice to see the error.
     
  5. Try simply parse your string with
    Code (Text):
    ChatColour.translateAlternateColourCodes('&', string)
    It is much more user-friendly and almost anyone that plays minecraft servers should know what the colour codes mean.