So this is not formatting an is throwing an error.

Discussion in 'Plugin Development' started by Hyperenci, Jul 24, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    Hyperenci

    Ok so im doing a conversion between hexadecimal base 16 an decimal base 10 and for some reason whenever i format it so it has ",(s)" it throws an error and i cant figure out how to fix it, here is my source code.
    Code:
        private boolean hexToDec(CommandSender sender, String[] args) {
            Player player = (Player) sender;
            if(args.length == 0) {
                player.sendMessage(ChatColor.GREEN + " Please put in a value to convert.");
            }else{
               
                try {
                    long ConvertHTD = Long.parseLong(args[0], 16);
                    String ConvertedHTD = Long.toString(ConvertHTD, 10);
                    String ConvertHTBF = new DecimalFormat("###,###,###,###,###,###,###,###,###,###,###,###").format(ConvertedHTD);
                    player.sendMessage(ChatColor.AQUA + args[0] + ChatColor.GREEN + " in Hexadecimal converted to Decimal is " + ChatColor.AQUA + ConvertHTBF);
                }catch(NumberFormatException exception) {
                    player.sendMessage(ChatColor.AQUA + "[Calc] " + ChatColor.RED + "That is not a valid problem, please only use numbers.");
                }
            }
            return true;
        }
    
    
     
    Last edited: Jul 24, 2016
  2. Offline

    AlvinB

  3. Offline

    Hyperenci

    Yes. let me get the error for you. And o sorry that is the working code, here is the error throwing code.
    Code:
        private boolean hexToDec(CommandSender sender, String[] args) {
            Player player = (Player) sender;
            if(args.length == 0) {
                player.sendMessage(ChatColor.GREEN + " Please put in a value to convert.");
            }else{
              
                try {
                    long ConvertHTD = Long.parseLong(args[0], 16);
                    String ConvertedHTD = Long.toString(ConvertHTD, 10);
                    String ConvertHTBF = new DecimalFormat("###,###,###,###,###,###,###,###,###,###,###,###").format(ConvertedHTD);
                    player.sendMessage(ChatColor.AQUA + args[0] + ChatColor.GREEN + " in Hexadecimal converted to Decimal is " + ChatColor.AQUA + ConvertHTBF);
                }catch(NumberFormatException exception) {
                    player.sendMessage(ChatColor.AQUA + "[Calc] " + ChatColor.RED + "That is not a valid problem, please only use numbers.");
                }
            }
            return true;
        }
    
    
    So i cant get the error but it says error and that it is null?
     
    Last edited: Jul 24, 2016
  4. Offline

    mine-care

    @Hyperenci Is it a nullpointerexception? Read this
    Also in the bits of code you provided, don't cast blindly, and follow naming conventions thus making the code easier to read ;)
     
  5. Offline

    Hyperenci

    Also im not casting blindly im checking if the player is a player earlier in my code, my class has over 800 lines of code and thats why i only pasted the method that was throwing errors. And all it says is error : null
     
  6. Offline

    bwfcwalshy Retired Staff

    @Hyperenci Why do you check for player and then use a CommandSender arg? Then you just end up recasting, seems a bit pointless.
     
    mine-care likes this.
  7. Offline

    Hyperenci

    It was just the way i had it set up/
     
  8. Offline

    mine-care

    Couldn't have known that, although i agree with @bwfcwalshy that it seems a litle pointless :/
    There should be a stack trace, unless the exception is handled and only the message is displayed. Thats why we keep asking for the error :p Without it, all i can tell you is that something is null which isnt really helpfull is it.
     
  9. Offline

    Hyperenci

    Ok so now it is showing a stacktrace and ill post it here.

    C:\Users\Hank\Desktop\Files\Server\Bukkit and Plugins\Offline-Server>java -Xms1024M -Xmx1024M -jar craftbukkit-1.8.8.jar
    Loading libraries, please wait...
    [01:19:03 INFO]: Starting minecraft server version 1.8.8
    [01:19:03 INFO]: Loading properties
    [01:19:03 INFO]: Default game type: CREATIVE
    [01:19:03 INFO]: Generating keypair
    [01:19:04 INFO]: Starting Minecraft server on *:25565
    [01:19:04 INFO]: Using default channel type
    [01:19:04 INFO]: This server is running CraftBukkit version git-Bukkit-efe04b8 (MC: 1.8.8) (Implementing API version 1.8.8-R0.1-SNAPSHOT)
    [01:19:04 INFO]: [RedUtil] Loading RedUtil v0.24
    [01:19:04 INFO]: [DeathRandomSpawn] Loading DeathRandomSpawn v1.0
    [01:19:04 WARN]: **** SERVER IS RUNNING IN OFFLINE/INSECURE MODE!
    [01:19:04 WARN]: The server will make no attempt to authenticate usernames. Beware.
    [01:19:04 WARN]: While this makes the game possible to play without internet access, it also opens up the ability for hackers to connect with any username they choose.
    [01:19:04 WARN]: To change this, set "online-mode" to "true" in the server.properties file.
    [01:19:04 INFO]: Preparing level "world"
    [01:19:05 INFO]: Preparing start region for level 0 (Seed: -949258421629280314)
    [01:19:06 INFO]: Preparing spawn area: 31%
    [01:19:07 INFO]: Preparing spawn area: 76%
    [01:19:07 INFO]: Preparing start region for level 1 (Seed: -949258421629280314)
    [01:19:08 INFO]: Preparing spawn area: 48%
    [01:19:09 INFO]: Preparing spawn area: 89%
    [01:19:10 INFO]: Preparing start region for level 2 (Seed: -949258421629280314)
    [01:19:10 INFO]: [RedUtil] Enabling RedUtil v0.24
    [01:19:10 INFO]: RedUtils Enabled
    [01:19:10 INFO]: [DeathRandomSpawn] Enabling DeathRandomSpawn v1.0
    [01:19:10 INFO]: Server permissions file permissions.yml is empty, ignoring it
    [01:19:10 INFO]: Done (5.775s)! For help, type "help" or "?"
    >hextodec
    [01:19:35 INFO]: Please send the command from in-game.
    [01:21:23 INFO]: Dacotathebeast[/127.0.0.1:51356] logged in with entity id 154 at ([world]135.94839199288975, 109.60534639311507, 134.9796750861277)
    [01:21:31 WARN]: Can't keep up! Did the system time change, or is the server overloaded? Running 7749ms behind, skipping 154 tick(s)
    [01:21:40 INFO]: Dacotathebeast issued server command: /hectodec
    [01:21:41 INFO]: Dacotathebeast issued server command: /hextodec
    [01:21:42 WARN]: Can't keep up! Did the system time change, or is the server overloaded? Running 2284ms behind, skipping 45 tick(s)
    [01:21:45 INFO]: Dacotathebeast issued server command: /hextodec F
    [01:21:45 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'hextodec' in plugin RedUtil v0.24
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
    at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:140) ~[craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
    at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:620) ~[craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
    at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1106) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
    at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:966) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:37) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
    at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(SourceFile:9) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
    at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
    at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_91]
    at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_91]
    at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
    at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:673) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
    at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:335) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
    at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:629) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
    at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:537) [craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
    at java.lang.Thread.run(Unknown Source) [?:1.8.0_91]
    Caused by: java.lang.IllegalArgumentException: Cannot format given Object as a Number
    at java.text.DecimalFormat.format(Unknown Source) ~[?:1.8.0_91]
    at java.text.Format.format(Unknown Source) ~[?:1.8.0_91]
    at me.coderbrik.red.Main.hexToDec(Main.java:145) ~[?:?]
    at me.coderbrik.red.Main.onCommand(Main.java:68) ~[?:?]
    at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[craftbukkit-1.8.8.jar:git-Bukkit-efe04b8]
    ... 15 more
    >
     
  10. Offline

    mine-care

    Aha! So the error isn't null afterall, it is an IllegalArgumentException thrown in the method hexToDecimal (line 145 of Main) because the provided parameter can not be formated to a number :p
    The param may be null and thats what the error is 'trying to say'
     
  11. Offline

    ZeldoKavira Administrator Curse

    We do not support offline mode servers.
     
Thread Status:
Not open for further replies.

Share This Page