Uploaded image for project: 'Commons Lang'
  1. Commons Lang
  2. LANG-75

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

    Details

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

      Operating System: other
      Platform: Other

    • Bugzilla Id:
      21904

      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

        Attachments

          Activity

            People

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

              Dates

              • Created:
                Updated:
                Resolved: