Solved Can't catch NumberFormatException

Discussion in 'Spigot Plugin Development' started by Menfie, May 11, 2016.

  1. I got this subcommand and when I type /aa add "String" I get NumberFormatException on chat but not on console.

    Code (Text):
    else if (args.length >= 2) {

                    String subcommand = args[0];
                    Integer money = Integer.parseInt(args[1]);
                    if (subcommand.equalsIgnoreCase("add")) {

                        if (money != null) {

                            try {
                                money = Integer.parseInt(args[1]);
                            } catch (NumberFormatException ex) {
                                commandSender.sendMessage("§c§l(!) §cWrong usage:§7 /aa add #");
                            }
                            commandSender.sendMessage("Working!");

                        }

                    } else {
                        commandSender.sendMessage("§c§l(!) §cWrong usage:§7 /aa add #");
                    }

                }
     
  2. Use:
    Code (Text):
    Integer.parseInt(args[1]);
    instead of the money = one.
     
    • Informative Informative x 1
  3. Thanks for your help. But another error:

    Code (Text):
    null
    org.bukkit.command.CommandException: Unhandled exception executing command 'aa' in plugin AA v1.0.0
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[server.jar:git-Spigot-e4d4710-e1ebe52]
        at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[server.jar:git-Spigot-e4d4710-e1ebe52]
        at com.massivecraft.massivelag.inject.command.MassiveLagCommandMap.dispatch(MassiveLagCommandMap.java:59) ~[?:?]
        at org.bukkit.craftbukkit.v1_8_R3.CraftServer.dispatchCommand(CraftServer.java:641) ~[server.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.PlayerConnection.handleCommand(PlayerConnection.java:1162) [server.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:997) [server.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:45) [server.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.PacketPlayInChat.a(PacketPlayInChat.java:1) [server.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [server.jar:git-Spigot-e4d4710-e1ebe52]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_66]
        at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_66]
        at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [server.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [server.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [server.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [server.jar:git-Spigot-e4d4710-e1ebe52]
        at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [server.jar:git-Spigot-e4d4710-e1ebe52]
        at java.lang.Thread.run(Thread.java:745) [?:1.8.0_66]
    Caused by: java.lang.NumberFormatException: For input string: "1s"
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[?:1.8.0_66]
        at java.lang.Integer.parseInt(Integer.java:580) ~[?:1.8.0_66]
        at java.lang.Integer.parseInt(Integer.java:615) ~[?:1.8.0_66]
        at com.wtfactions.aa.Commands.onCommand(Commands.java:51) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[server.jar:git-Spigot-e4d4710-e1ebe52]
        ... 16 more
     
  4. You're trying to convert "1s" to a number, 's' can not be converted to a number.
     
    • Informative Informative x 1
  5. It's my input /aa add 1s for testing.
     
  6. Since "1s" is not a number, (1 is), it is throwing the NumberFormatException. After the "Catch" add a return; statement.
     
    • Informative Informative x 1
  7. Code (Text):
                            try {
                               Integer.parseInt(args[1]);
                            } catch (NumberFormatException ex) {
                                commandSender.sendMessage("§c§l(!) §cWrong usage:§7 /aa add #");
    return;
                            }
                            commandSender.sendMessage("Working!");
         

                        }
    This will prevent the error from showing.
     
    • Informative Informative x 1
  8. Also you dont try catch at line 4
    Code (Text):
     Integer money = Integer.parseInt(args[1]);
     
    • Informative Informative x 1
  9. But return needs a boolean right? (sorry I'm a little dumb)
     
  10. return does not need to be anything. The return type srictly depends on what your method return type is. If your method return type is void, you can just use return;

    Methods that return integers need return *SOME_INTEGER*; in the statment
    Methods that return string need return *SOME_STRING*; in the statement

    However, because the onCommand returns a boolean, you will need to return either true or false, it doesn't matter which one in this case.
     
    • Informative Informative x 1
  11. I just added return true; it didn't work then I moved the catch to line 4 as @Orinion77 suggested and it's working thanks to you guys :)
     
  12. Mark the thread as solved please :)
     
    • Funny Funny x 1