Solved Unexpected NPE on command execution

Discussion in 'Spigot Plugin Development' started by illuminator3, Feb 3, 2020.

Thread Status:
Not open for further replies.
  1. Every time I execute my command I get this NPE:

    Code (Java):
    [19:23:17 INFO]: null
    [19:23:17 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'build' in plugin Lobby v1.0-SNAPSHOT
            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(Executors.java:511) [?:1.8.0_222]
            at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_222]
            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(Thread.java:748) [?:1.8.0_222]
    Caused by: java.lang.NullPointerException

    Any ideas as to why this happens?
     
  2. It's because you have a NPE in your code.
     
    • Agree Agree x 1
  3. I think you should really show us the code
     
  4. I actually found my problem while doing some debugging.
    So basically I can't get certain strings out of a yaml config file.

    file.png
     
  5. Why won't they work? Try using a yaml parser.
    None of them work? Really?
     
  6. show the full error. post your yml as text, not an image that we cant interact with
     
    • Like Like x 2
  7. I already showed the full stack trace.

    Here's a part of my yaml:

    Code (YAML):
    messages:

      command
    :

        nopermissions
    : "&8\u2759 &6BlockRain &8\u2758 &7Du besitzt nicht die benötigten Rechte um dies zu tun"
        playersonly
    : "&8\u2759 &6BlockRain &8\u2758 &7Dieser Befehl ist nur für Spieler"
        notonline
    : "&8\u2759 &6BlockRain &8\u2758 &7cDieser spieler befindet sich nicht auf dem Server"
        unknown
    : "&8\u2759 &6BlockRain &8\u2758 &cDieser Befehl existiert nicht!"

        build
    :
          on
    : "&8\u2759 &6BlockRain &8\u2758 &aDu bist nun im &e&lBUILD &aModus!"
          off
    : "&8\u2759 &6BlockRain &8\u2758 &cDu bist nun nicht mehr im &e&lBUILD &cModus!"
     
     
  8. No, what you posted is not the full stacktrace
     
  9. It is lol
    Code (Java):
    [19:22:48] [Server thread/INFO]: illuminator3 issued server command: /build
    [19:22:48] [Server thread/INFO]: null
    [19:22:48] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'build' in plugin Lobby v1.0-SNAPSHOT
        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(Executors.java:511) [?:1.8.0_222]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_222]
        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(Thread.java:748) [?:1.8.0_222]
    Caused by: java.lang.NullPointerException
    This is literally the entire stack trace, trust me.
     
  10. Caused by: java.lang.NullPointerException
    there is a large chunk of text after this that tells where the error came from. its not there. youre missing a part of the trace
     
    • Like Like x 1
  11. It should continue from here and show what lines the error is
     
  12. show us where youre handling the build command. thats a very vague start to where the error would be. show the entire class.
     
  13. I found the problem (last time it actually didn't show the entire stack trace):

    Code (Java):
        private static FileConfiguration messages = getFileConfiguration("messages");

        public String getString(@NotNull final String path)
        {
            return ChatColor.translateAlternateColorCodes('&', messages.getString("messages." + path)); // <--- this line produces a NPE cuz the string "isn't" in the fileconfig but it is
        }
     
  14. if that line is producing the NPE, then messages is null. show getFileConfiguration method. im thinking thats not the line that throws the NPE tho. likely ChatColor#translate throws it because youre providing a null string. print the paths until you get an error and see what it is.
     
  15. Code (Java):
    package net.blockrain.lobby.strings;

    import net.blockrain.lobby.Lobby;
    import org.bukkit.ChatColor;
    import org.bukkit.configuration.InvalidConfigurationException;
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.configuration.file.YamlConfiguration;
    import org.bukkit.plugin.java.JavaPlugin;
    import org.jetbrains.annotations.NotNull;

    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.FileOutputStream;

    public class Strings
    {
        private static FileConfiguration messages = getFileConfiguration("messages");

        public String getString(@NotNull final String path)
        {
            return ChatColor.translateAlternateColorCodes('&', messages.getString("messages." + path));
        }

        @NotNull
        public static FileConfiguration getFileConfiguration(String fileName)
        {
            File file = new File("plugins/Lobby/" + fileName + ".yml");
            FileConfiguration fileConfiguration = new YamlConfiguration();

            try
            {
                fileConfiguration.load(file);
            } catch (IOException | InvalidConfigurationException ex)
            {
                ex.printStackTrace();
            }

            final JavaPlugin plugin = Lobby.getPlugin();

            try
            {
                if (!file.exists())
                {
                    file.getParentFile().mkdirs();
                    InputStream in = plugin.getResource(fileName + ".yml");
                    OutputStream out = new FileOutputStream(file);
                    byte[] buf = new byte[1024];
                    int len;
                    while ((len = in.read(buf)) > 0) out.write(buf, 0, len);
                    out.close();
                    in.close();
                }

                fileConfiguration.load(file);
            } catch(IOException | InvalidConfigurationException ex)
            {
                plugin.getLogger().severe("Plugin unable to write configuration file " + fileName + ".yml!");
                plugin.getLogger().severe("Disabling...");
                plugin.getServer().getPluginManager().disablePlugin(plugin);

                ex.printStackTrace();
            }

            return fileConfiguration;
        }
    }
     
  16. why dont you use the bukkit methods for this?
    JavaPlugin#saveResource would write a non-existent file to where you want it to if its packaged with your jar.
    YamlConfiguration#loadConfiguration(File) loads it for you automatically.
    file = new File(JavaPlugin#getDataFolder(), "fileName.yml")
    if(!file.exists) JavaPlugin#saveResource("fileName.yml", false)
    config = YamlConfiguration#loadConfiguration(file)

    dont see anything null there. thats not whats throwing the NPE. probably passing a null value. print your paths youre using as youre getting them from the config and see which ones are returning null and why.
     
  17. Strahan

    Benefactor

    Also just an aside,
    Code (Text):
    public String getString(@NotNull final String path)
    {
        return ChatColor.translateAlternateColorCodes('&', messages.getString("messages." + path));
    }
    That could throw an NPE if an invalid path is passed. You should either pull the String from config then validate it, or pass a default.
     
  18. Just realized that I still have that old code in my sources. I used to have an UTF8FileConfiguration class and I needed to read the file myself.
    Now I'm using the methods you said, but it's still not working.
     
  19.  
  20. The path is the same as in the YAML. But it's still not working.
     
Thread Status:
Not open for further replies.