Solved NumberFormatException

Discussion in 'Plugin Development' started by MCForger, Dec 31, 2012.

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

    MCForger

    Hello Bukkit Community!
    I am trying to make a string to location method. It works until the point for it gets an NPE error on line 73 which is right after the { of the try!
    Code:
        public static Location stringToLoc(String s)
        {
            try
            {
            String[] args = s.split(",");
            String wName = args[0];
            wName = wName.replace("CraftWorld", "").replace("Location", "").replace("name", "").replace("{", "").replace("}", "").replace("world==", "");
            String x = args[1];
            x = x.replace("x=", "");
            String y = args[2];
            y = y.replace("y=", "");
            String z = args[3];
            z = z.replace("z=", "");
            double arg1 = Integer.valueOf(x).doubleValue();
            double arg2 = Integer.valueOf(y).doubleValue();
            double arg3 = Integer.valueOf(z).doubleValue();
            Location location = new Location(Bukkit.getWorld(wName), arg1, arg2, arg3);
            return location;
            }
            catch(NumberFormatException e)
            {
     
            }
            return null;
        }
    Any help or suggestions will be helpful.
    Happy New Years as well!
    Thank you.
     
  2. Offline

    tommycake50

    if its an NPE from what i can see you are passing a null string into the method...
    Also dont use static its not there for convenience.
     
  3. Offline

    Tirelessly

    Try Double.parseDouble instead of Integer.valueOf().doubleValue() ?

    That's pretty ignorant, there's no reason a static method wouldn't be appropriate.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
    MCForger likes this.
  4. Offline

    tommycake50

    maybe.
    it depends if its his main class or not.
     
  5. Offline

    MCForger

    Well answering two questions!
    #1 Not in my main class in fact 3 other classes call this function. I like organizing were I put my methods I made =P
    #2 Tried the other one no different but I Believe my error is occurring in the for loop that feeds this here is the code for that. I believe its out I stated the for loop.
    Code:
        public static void diamondDropParty()
        {
            List<String> tempL = API.getConfigs().getPropertyList(ConfFile.KKDD, "DiamondDP");
            for (String l : tempL)
            {
                Location loc = DiamondDropPartyCommand.stringToLoc(l);
                ItemStack dItem = new ItemStack(Material.DIAMOND, 2);
                loc.getWorld().dropItemNaturally(loc, dItem);
            }
            return;
        }
    I believe I need to iterate or such over each location in that collection?
     
  6. Offline

    tommycake50

    btw why are you using static and not instance methods?
     
  7. Offline

    fireblast709

    in this kind of util methods, that don't use any variables beside whats inside the method, static is a pretty good choice
     
  8. Offline

    MCForger

    Cause that is just my preference.
    But lets get off static topic and move to the NPE error please.
     
  9. Offline

    fireblast709

    just right at the start of the foreach
    Code:
    if(l == null)
    {
        continue;
    }
    as String can be null
     
  10. Offline

    tommycake50

    i suppose.
    but you shouldn't get into a habit of using static.
     
  11. Offline

    MCForger

    No. I know were the error is coming from the fact that I am trying to make a WHOLE string list into one string. I wanted to go through that list one at a time. Since each input is a location for a diamond drop. How would I iterate through this list?
     
  12. Offline

    tommycake50

    using a foreach loop?
    for(object o : list){
    }
     
  13. Offline

    fireblast709

    That is why I specified why it is a good use in this particular situation
    MCForger your code already iterated over the list, I don't see the problem (if you apply the null check)
     
  14. Offline

    Tirelessly

    There is no reason not to use static here. There's a time and place for static, just because somebody on the forums said that using static objects causes memory leaks doesn't mean a static method is wrong to use.
     
  15. Offline

    MCForger

    fireblast709
    It still throws an NPE error with this code:
    Code:
        public static void diamondDropParty()
        {
            List<String> tempL = KingKraftAPI.getConfigs().getPropertyList(KingKraftConfFile.KKDD, "DiamondDP");
            for (String l : tempL)
            {
              if (l == null)
              {
                  return;
              }
              Location loc = DiamondDropPartyCommand.stringToLoc(l);
              ItemStack dItem = new ItemStack(Material.DIAMOND, 2);
              loc.getWorld().dropItemNaturally(loc, dItem);
            }
            KingKraftChat.logInfo("Diamond Drop Party Went Correctly!");
            return;
        }
    On line #77 which is KingKraftChat.logInfo("Diamond Drop Party Went Correctly!");
    Very confused and tired atm and again please stop with the whole static thing.
     
  16. Offline

    fireblast709

    KingKraftChat is null?
     
  17. Offline

    MCForger

    No. I think it is just bugging out. That gets enabled before that and works fine on other commands and such calling the same thing from KingKraftChat. So I know that isnt null.
    SORRY
    This is saying there is an NPE
    loc.getWorld().dropItemNaturally(loc, dItem);
     
  18. Offline

    fireblast709

    loc.getWorld() is null
     
  19. Offline

    MCForger

    fireblast709
    Alright this is fixed. Now that remaining problem is with taking in such a big number or in this case not a number for the x, y, and z value. How should I fix this but still get rid of the "x=" or "y=" bit that I am receiving?

    Never mind this is solved! The funny thing is we solved problems that were not the main problem and the main problem was solved with the comment I did not see by Tirelessly
    So thank you everyone this is now solved!
    This is the updated code for any to use:
    Code:
        public static Location stringToLoc(String s)
        {
            try
            {
            String[] args = s.split(",");
            String wName = args[0];
            wName = wName.replace("CraftWorld", "").replace("Location", "").replace("name", "").replace("{", "").replace("}", "").replace("world==", "");
            String x = args[1];
            x = x.replace("x=", "");
            String y = args[2];
            y = y.replace("y=", "");
            String z = args[3];
            z = z.replace("z=", "");
            double arg1 = Double.parseDouble(x);/*Integer.valueOf(x).doubleValue()*/;
            double arg2 = Double.parseDouble(y);
            double arg3 = Double.parseDouble(z);
            Location location = new Location(Bukkit.getWorld(wName), arg1, arg2, arg3);
            return location;
            }
            catch(NumberFormatException e)
            {
                KingKraftChat.logSevere("We had a slight problem: " + e);
            }
            return null;
        }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 30, 2016
Thread Status:
Not open for further replies.

Share This Page