]> granicus.if.org Git - icu/commitdiff
ICU-13227 Throwing NullPointerException on string methods in DecimalFormat
authorShane Carr <shane@unicode.org>
Thu, 15 Jun 2017 01:45:12 +0000 (01:45 +0000)
committerShane Carr <shane@unicode.org>
Thu, 15 Jun 2017 01:45:12 +0000 (01:45 +0000)
X-SVN-Rev: 40170

icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java
icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java

index e1f5acef3d4d79da9567560252636e8a4bf54a74..4d9602241904eac45bc3164da5993c42914e4c43 100644 (file)
@@ -888,6 +888,9 @@ public class DecimalFormat extends NumberFormat {
    * @stable ICU 2.0
    */
   public synchronized void setPositivePrefix(String prefix) {
+    if (prefix == null) {
+      throw new NullPointerException();
+    }
     properties.setPositivePrefix(prefix);
     refreshFormatter();
   }
@@ -918,12 +921,15 @@ public class DecimalFormat extends NumberFormat {
    * <p>Using this method overrides the affix specified via the pattern, and unlike the pattern, the
    * string given to this method will be interpreted literally WITHOUT locale symbol substitutions.
    *
-   * @param suffix The literal string to prepend to negative numbers.
+   * @param prefix The literal string to prepend to negative numbers.
    * @category Affixes
    * @stable ICU 2.0
    */
-  public synchronized void setNegativePrefix(String suffix) {
-    properties.setNegativePrefix(suffix);
+  public synchronized void setNegativePrefix(String prefix) {
+    if (prefix == null) {
+      throw new NullPointerException();
+    }
+    properties.setNegativePrefix(prefix);
     refreshFormatter();
   }
 
@@ -958,6 +964,9 @@ public class DecimalFormat extends NumberFormat {
    * @stable ICU 2.0
    */
   public synchronized void setPositiveSuffix(String suffix) {
+    if (suffix == null) {
+      throw new NullPointerException();
+    }
     properties.setPositiveSuffix(suffix);
     refreshFormatter();
   }
@@ -993,6 +1002,9 @@ public class DecimalFormat extends NumberFormat {
    * @stable ICU 2.0
    */
   public synchronized void setNegativeSuffix(String suffix) {
+    if (suffix == null) {
+      throw new NullPointerException();
+    }
     properties.setNegativeSuffix(suffix);
     refreshFormatter();
   }
@@ -2473,6 +2485,9 @@ public class DecimalFormat extends NumberFormat {
    * @see PatternString#parseToExistingProperties
    */
   void setPropertiesFromPattern(String pattern, int ignoreRounding) {
+    if (pattern == null) {
+      throw new NullPointerException();
+    }
     PatternString.parseToExistingProperties(pattern, properties, ignoreRounding);
   }
 
index 6c072b9bfb1824b561ec12ad28fdffb4f010bbc2..f4fc9948d0b0428d91d8467957959625647771b2 100644 (file)
@@ -19,6 +19,7 @@ import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
+import java.lang.reflect.InvocationTargetException;
 import java.math.BigInteger;
 import java.math.RoundingMode;
 import java.text.AttributedCharacterIterator;
@@ -5312,6 +5313,51 @@ public class NumberFormatTest extends TestFmwk {
         }
     }
 
+    @Test
+    public void testStringMethodsNPE() {
+        String[] npeMethods = {
+                "applyLocalizedPattern",
+                "applyPattern",
+                "setNegativePrefix",
+                "setNegativeSuffix",
+                "setPositivePrefix",
+                "setPositiveSuffix"
+        };
+        for (String npeMethod : npeMethods) {
+            DecimalFormat df = new DecimalFormat();
+            try {
+                DecimalFormat.class.getDeclaredMethod(npeMethod, String.class).invoke(df, (String) null);
+                fail("NullPointerException not thrown in method " + npeMethod);
+            } catch (InvocationTargetException e) {
+                assertTrue("Exception should be NullPointerException in method " + npeMethod,
+                        e.getCause() instanceof NullPointerException);
+            } catch (Exception e) {
+                // Other reflection exceptions
+                throw new AssertionError("Reflection error in method " + npeMethod, e);
+            }
+        }
+
+        // Also test the constructors
+        try {
+            new DecimalFormat(null);
+            fail("NullPointerException not thrown in 1-parameter constructor");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+        try {
+            new DecimalFormat(null, new DecimalFormatSymbols());
+            fail("NullPointerException not thrown in 2-parameter constructor");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+        try {
+            new DecimalFormat(null, new DecimalFormatSymbols(), CurrencyPluralInfo.getInstance(), 0);
+            fail("NullPointerException not thrown in 4-parameter constructor");
+        } catch (NullPointerException e) {
+            // Expected
+        }
+    }
+
     @Test
     public void testParseGroupingMode() {
         ULocale[] locales = {         // GROUPING   DECIMAL