Commons Lang
  1. Commons Lang
  2. LANG-75

NumberUtils.createBigDecimal("") NPE in Sun 1.3.1_08

    Details

    • Type: Bug Bug
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.0
    • Component/s: None
    • Labels:
      None
    • Environment:

      Operating System: other
      Platform: Other

      Description

      Hello,

      The NumberUtils.createXXX methods all have the following pattern:

      public static XXX createXXX(String str) {
      if (str == null)

      { return null; }

      In the case of BigDecimal, passing in a "" to new BigDecimal(String) in Sun
      1.3.1_08 blows up like this:

      java.lang.StringIndexOutOfBoundsException: String index out of range: 0
      at java.lang.String.charAt(String.java:582)
      at java.math.BigDecimal.<init>(BigDecimal.java:124)
      at org.apache.commons.lang.math.NumberUtils.createBigDecimal(NumberUtils.java:4
      78)
      at org.apache.commons.lang.math.NumberUtilsTest.testCreateBigDecimal(NumberUtil
      sTest.java:209)

      Under Sun 1.4.2, you get a NumberFormatException if the length of the string is
      0 (no trim()). The unit tests expect a NumberFormatException when you pass in "".

      So... to make this all nice on 1.3, should all of the guard clauses become:

      (1)

      if (StringUtil.isEmpty(str)) { return null; }

      (2)

      if (StringUtil.isBlank(str))

      { return null; }

      (3)

      if (str == null) { return null; }

      if (StringUtil.isEmpty(str))

      { return str; }

      ?

      I think (2) would be good since it would not blow up on "" AND " " but I am not
      familiar with the various invocation contexts, so, please opine.

      Thanks,
      Gary

        Activity

        No work has yet been logged on this issue.

          People

          • Assignee:
            Unassigned
            Reporter:
            Gary Gregory
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development