Solved Placeholders and NullPointers

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

  1. iam trying to get the my config to use placeholders but i keep on getting nullpointers
    Here is part of my code
    Code (Text):

        public String formatTime(double seconds)
        {
            int Sec = (int) (seconds % 60);
            int Min = (int) Math.floor(seconds / 60) % 60;
            int Hour = (int) Math.floor(seconds / 3600) % 24;
            int Day = (int) Math.floor(seconds / 86400);
           
            String sConfig = getConfig().getString("Seconds");
            sConfig = sConfig.replaceAll("%s%", Sec + "");
            sConfig = sConfig.replaceAll("%m%", Min + "");
            sConfig = sConfig.replaceAll("%h%", Hour + "");
            sConfig = sConfig.replaceAll("%d%", Day + "");          
            String mConfig = getConfig().getString("Minuties");
            mConfig = mConfig.replaceAll("%s%",  Sec + "");
            mConfig = mConfig.replaceAll("%m%",  Min + "");
            mConfig = mConfig.replaceAll("%h%",  Hour + "");
            mConfig = mConfig.replaceAll("%d%",  Day + "");
            String hConfig = getConfig().getString("Hours");
            hConfig = hConfig.replaceAll("%s%",  Sec + "");
            hConfig = hConfig.replaceAll("%m%",  Min + "");
            hConfig = hConfig.replaceAll("%h%",  Hour + "");
            hConfig = hConfig.replaceAll("%d%",  Day + "");
            String dConfig = getConfig().getString("Days");
            dConfig = dConfig.replaceAll("%s%",  Sec + "");
            dConfig = dConfig.replaceAll("%m%",  Min + "");
            dConfig = dConfig.replaceAll("%h%",  Hour + "");
            dConfig = dConfig.replaceAll("%d%",  Day + "");
           
           
            String output = "";
            if (seconds > 86399 ) output = dConfig;
            else if (seconds > 3599 ) output = hConfig;
            else if (seconds > 59) output = mConfig;
            else output = sConfig;                      
            return output;      
        }


        public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
        {
            if (command.getName().equalsIgnoreCase("formattime"))
            {
                player.sendMessage(formatTime((int)Double.parseDouble(args[0])));
                return true;
            }
    }
     
  2. where do you receive the error?
    Maybe is "getConfig()" null? Would be good to post the error line that appeared ( or just the full error also )
     
    • Like Like x 1
  3. Here is the error

    Code (Text):

    [17:19:46 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'formattime' in plugin FormatterAPI vAlpha Build 50
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-9ba269b-c19c293]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-9ba269b-c19c293]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:650) ~[spigot.jar:git-Spigot-9ba269b-c19c293]
            at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1354) [spigot.jar:git-Spigot-9ba269b-c19c293]
            at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1189) [spigot.jar:git-Spigot-9ba269b-c19c293]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-9ba269b-c19c293]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-9ba269b-c19c293]
            at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-9ba269b-c19c293]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_101]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_101]
            at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot.jar:git-Spigot-9ba269b-c19c293]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot.jar:git-Spigot-9ba269b-c19c293]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-9ba269b-c19c293]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot.jar:git-Spigot-9ba269b-c19c293]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot.jar:git-Spigot-9ba269b-c19c293]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    Caused by: java.lang.NullPointerException
            at yoreni.FormatterAPI.main.FormatterAPI.formatTime(FormatterAPI.java:39) ~[?:?]
            at yoreni.FormatterAPI.main.FormatterAPI.onCommand(FormatterAPI.java:119) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-9ba269b-c19c293]
            ... 15 more
     
    Here is line 39
    Code (Text):

    sConfig = sConfig.replaceAll("%s%", Sec + "");
     
    Here is line 119
    Code (Text):

    player.sendMessage(formatTime((int)Double.parseDouble(args[0])));
    [CODE]
     
  4. Ok so your error appeared in the first line:

    Code (Text):

    String sConfig = getConfig().getString("Seconds");
            sConfig = sConfig.replaceAll("%s%", Sec + "");
     
    So that basically means that getConfig() is null or your String in the config is null.
     
    • Like Like x 1
  5. Its probably the string in my config is null cos my config is not updating to the new one
    I have now fixed the problem i didn't put the "" around the thing in the config
    Thank you for your help