NumberFormatException Help

Discussion in 'Spigot Plugin Development' started by JordanOsterberg, May 12, 2015.

  1. Hello Spigot, I have encountered a NumberFormatException while using a String[].
    Error:
    PHP:
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-1649]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-1649]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:514) [spigot.jar:git-Spigot-1649]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:499) [spigot.jar:git-Spigot-1649]
        at org.bukkit.craftbukkit.v1_7_R4.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:242) [spigot.jar:git-Spigot-1649]
        at org.bukkit.craftbukkit.v1_7_R4.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:212) [spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.PlayerConnection.a(PlayerConnection.java:636) [spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:50) [spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.PacketPlayInBlockPlace.handle(PacketPlayInBlockPlace.java:86) [spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.NetworkManager.a(NetworkManager.java:186) [spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.ServerConnection.c(ServerConnection.java:81) [spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.MinecraftServer.v(MinecraftServer.java:734) [spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.DedicatedServer.v(DedicatedServer.java:289) [spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.MinecraftServer.u(MinecraftServer.java:584) [spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.MinecraftServer.run(MinecraftServer.java:490) [spigot.jar:git-Spigot-1649]
        at net.minecraft.server.v1_7_R4.ThreadServerApplication.run(SourceFile:628) [spigot.jar:git-Spigot-1649]
    Caused by: java.lang.NumberFormatException: For input string: ""
        at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) ~[?:1.8.0_25]
        at java.lang.Integer.parseInt(Integer.java:592) ~[?:1.8.0_25]
        at java.lang.Integer.parseInt(Integer.java:615) ~[?:1.8.0_25]
        at com.thearchon.sgs.utils.Prizes.readPrizes(Prizes.java:22) ~[?:?]
    Code:
    PHP:
    Core plugin = Core.getCore();
       
        public ArrayList<Integer> readPrizes(Player player, String path)
        {
            List<String> pathList = (List<String>) Core.instance.getConfig().getList(path);
            ArrayList<Integer> newList = new ArrayList<Integer>();
            String prizes = plugin.getSQL().getPrizes(player);
            String[] indexList = prizes.split("\\,");
            for(int x = 0; x<indexList.length; x++)
            {
                int index = Integer.parseInt(indexList[x]);
                for(int y = 0; y<pathList.size(); y++)
                {
                    if(pathList.get(index).equals(pathList.get(y)))
                    {
                        newList.add(y);
                    }
                }
            }
            return newList;
        }
    The error is here: int index = Integer.parseInt(indexList[x]);
    Thank you!
     
  2. gigosaurus

    Supporter

    Integer.parseInt(indexList[x]) causing NumberFormatException => indexList[x] is not a valid integer.

    You're expecting your prizes string to be a list of integers separated by commas, but somewhere it either has two commas next to each other, or the string starts / ends with a comma.
    Both of these would be invalid:
    "3,4,5,"
    "4,,6,7"

    Also there's no need to escape "," so change .split("\\,") to .split(",")
     
    #2 gigosaurus, May 12, 2015
    Last edited: May 12, 2015
  3. Right now, the prizes string is just "" because I don't have anything there. So are you saying I need something in the prizes string such as "5" for this to work?

    Edit: Worked, thank you.
     
  4. gigosaurus

    Supporter

    Oh yeah, forgot about empty strings. Yes, that would work.