Solved NumberFormatError

Discussion in 'Spigot Plugin Development' started by EvanTheSurfer, Jun 16, 2016.

  1. I keep getting this error in console when the event is called:
    Code (Text):
    15.06 23:32:39 [Server] ERROR Could not pass event PlayerInteractEvent to CustomBankNotes v1.0
    15.06 23:32:39 [Server] INFO org.bukkit.event.EventException
    15.06 23:32:39 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot45.jar:git-Spigot-5f38d38-12698ea]
    15.06 23:32:39 [Server] INFO at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot45.jar:git-Spigot-5f38d38-12698ea]
    15.06 23:32:39 [Server] INFO at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot45.jar:git-Spigot-5f38d38-12698ea]
    15.06 23:32:39 [Server] INFO at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot45.jar:git-Spigot-5f38d38-12698ea]
    15.06 23:32:39 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [spigot45.jar:git-Spigot-5f38d38-12698ea]
    15.06 23:32:39 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:195) [spigot45.jar:git-Spigot-5f38d38-12698ea]
    15.06 23:32:39 [Server] INFO at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:191) [spigot45.jar:git-Spigot-5f38d38-12698ea]
    15.06 23:32:39 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:717) [spigot45.jar:git-Spigot-5f38d38-12698ea]
    15.06 23:32:39 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [spigot45.jar:git-Spigot-5f38d38-12698ea]
    15.06 23:32:39 [Server] INFO at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot45.jar:git-Spigot-5f38d38-12698ea]
    15.06 23:32:39 [Server] INFO at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot45.jar:git-Spigot-5f38d38-12698ea]
    15.06 23:32:39 [Server] INFO at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_66]
    15.06 23:32:39 [Server] INFO at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_66]
    15.06 23:32:39 [Server] INFO at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot45.jar:git-Spigot-5f38d38-12698ea]
    15.06 23:32:39 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.B(MinecraftServer.java:715) [spigot45.jar:git-Spigot-5f38d38-12698ea]
    15.06 23:32:39 [Server] INFO at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot45.jar:git-Spigot-5f38d38-12698ea]
    15.06 23:32:39 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot45.jar:git-Spigot-5f38d38-12698ea]
    15.06 23:32:39 [Server] INFO at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot45.jar:git-Spigot-5f38d38-12698ea]
    15.06 23:32:39 [Server] INFO at java.lang.Thread.run(Thread.java:745) [?:1.8.0_66]
    15.06 23:32:39 [Server] INFO Caused by: java.lang.NumberFormatException: For input string: "10000"
    15.06 23:32:39 [Server] INFO at sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:2043) ~[?:1.8.0_66]
    15.06 23:32:39 [Server] INFO at sun.misc.FloatingDecimal.parseDouble(FloatingDecimal.java:110) ~[?:1.8.0_66]
    15.06 23:32:39 [Server] INFO at java.lang.Double.parseDouble(Double.java:538) ~[?:1.8.0_66]
    15.06 23:32:39 [Server] INFO at java.lang.Double.valueOf(Double.java:502) ~[?:1.8.0_66]
    15.06 23:32:39 [Server] INFO at me.evanog.BankNotes.Main.onClickItem(Main.java:131) ~[?:?]
    15.06 23:32:39 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_66]
    15.06 23:32:39 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_66]
    15.06 23:32:39 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_66]
    15.06 23:32:39 [Server] INFO at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_66]
    15.06 23:32:39 [Server] INFO at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot45.jar:git-Spigot-5f38d38-12698ea]
    15.06 23:32:39 [Server] INFO ... 18 more
    I know which line is causing the problem I jjust cant figure out what the problem is. My code:
    Code (Text):
    public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
         
         
            if (sender instanceof Player) {
             
                Player p = (Player) sender;
             
                if (cmd.getName().equalsIgnoreCase("withdraw")) {
                 
                 
                    if (args.length == 1) {
                     
                     
                        if (isInt(args[0])) {
                         
                            int am = Integer.valueOf(args[0]);
                         
                            if (am <= econ.getBalance(p)) {
                             
                                BankNote bn = new BankNote(am);
                             
                                p.getInventory().addItem(bn.bankNote(am,p));
                                p.sendMessage(ChatColor.RED + "§l- " + bn.getAmount());
                                return true;
                             
                            }else {
                                p.sendMessage(ChatColor.RED + "You do not have enough money!");
                                return false;
                            }
                         
                         
                        }else {
                            sender.sendMessage(ChatColor.RED + "/withdraw <amount>");
                            return false;
                        }
                     
                     
                    }else {
                        sender.sendMessage(ChatColor.RED + "/withdraw <amount>");
                        return false;
                    }
                 
                }
            }
         
            return false;
        }
        //======================================EVENTS======================================================
     
        @EventHandler
        public void onClickItem(PlayerInteractEvent e) {
         
         
            Player p = (Player) e.getPlayer();
         
            if (e.getAction().equals(Action.RIGHT_CLICK_AIR)) {

                if (p.getItemInHand() != null && p.getItemInHand().hasItemMeta()) {
                 
                    if (p.getItemInHand().getItemMeta().getDisplayName().contains(ChatColor.GREEN + "§lBankNote §7(Right Click")) {
                            Bukkit.broadcastMessage("debug");
                          String lore = p.getItemInHand().getItemMeta().getLore().toString().replace('[', ' ').replace(']', ' ');
                          String [] lores = lore.split(":");
                          double money = Double.valueOf(lores[1]);
                       
                         
                            Main.econ.depositPlayer(p, money);
                          p.sendMessage(ChatColor.GREEN + "§l+ " + Double.valueOf(lores[1]));
                       
                         
               
                     
                     
                     
                    }
                 
                }
             
            }
         
         
        }
     
  2. Serializator

    Supporter

    Can you try Integer#parseInt() instead of Double#parseDouble(), because I think the problem is that it's an int and not a double.
     
  3. _Ug

    _Ug

    It also might be useful to show us line 131..
     
  4. Code (Text):
    double money = Double.valueOf(lores[1]);
     
  5. Well line 131 is dependent if there is 2 parameters after the command and also that it is a number, if it is not it will throw that error, so you have to make it do something else if the parameters are not.

    try:
    Code (Text):
    if( lore.length == 2 ){

                if( NumberUtils.isNumber( lore[1] ) ){
                   
                    //your code here
                   
                } else return;

            } else return;
     
  6. try {
    // Code
    } catch (NumberFormatException ev) {
    sender.sendMessage("Use only numbers.");
    }
     
    • Like Like x 1
  7. NumberFormatException is thrown when the value is not a valid number or of the wrong number type. In your case you are trying to parse doubles, which means it throw a NFE if you try to parse an integer. The two are numbers, but wont parse the same.
     
  8. I just ended up using regex. It works now. Solved