8217254: CompactNumberFormat:: CompactNumberFormat​() constructor does not comply with spec
8217721: CompactNumberFormat:: format​() method spec for IAEx is not complaint

Reviewed-by: naoto
diff --git a/src/java.base/share/classes/java/text/CompactNumberFormat.java b/src/java.base/share/classes/java/text/CompactNumberFormat.java
index 9cb43fc..c7fe484 100644
--- a/src/java.base/share/classes/java/text/CompactNumberFormat.java
+++ b/src/java.base/share/classes/java/text/CompactNumberFormat.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -406,6 +406,11 @@
     public final StringBuffer format(Object number,
             StringBuffer toAppendTo,
             FieldPosition fieldPosition) {
+
+        if (number == null) {
+            throw new IllegalArgumentException("Cannot format null as a number");
+        }
+
         if (number instanceof Long || number instanceof Integer
                 || number instanceof Short || number instanceof Byte
                 || number instanceof AtomicInteger
@@ -1053,6 +1058,11 @@
      */
     private void applyPattern(String pattern, int index) {
 
+        if (pattern == null) {
+            throw new IllegalArgumentException("A null compact pattern" +
+                    " encountered at index: " + index);
+        }
+
         int start = 0;
         boolean gotNegative = false;
 
diff --git a/test/jdk/java/text/Format/CompactNumberFormat/TestCompactNumber.java b/test/jdk/java/text/Format/CompactNumberFormat/TestCompactNumber.java
index 55e95da..45f5de4 100644
--- a/test/jdk/java/text/Format/CompactNumberFormat/TestCompactNumber.java
+++ b/test/jdk/java/text/Format/CompactNumberFormat/TestCompactNumber.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,7 +22,7 @@
  */
 /*
  * @test
- * @bug 8177552
+ * @bug 8177552 8217721
  * @summary Checks the functioning of compact number format
  * @modules jdk.localedata
  * @run testng/othervm TestCompactNumber
@@ -528,6 +528,11 @@
                 .getCompactNumberInstance(l, NumberFormat.Style.LONG).format(10000));
     }
 
+    @Test(expectedExceptions = IllegalArgumentException.class)
+    public void testFormatWithNullParam() {
+        FORMAT_EN_US_SHORT.format(null);
+    }
+
     @Test(dataProvider = "format")
     public void testFormat(NumberFormat cnf, Object number,
             String expected) {
diff --git a/test/jdk/java/text/Format/CompactNumberFormat/TestCompactPatternsValidity.java b/test/jdk/java/text/Format/CompactNumberFormat/TestCompactPatternsValidity.java
index 962393e..8fb0d64 100644
--- a/test/jdk/java/text/Format/CompactNumberFormat/TestCompactPatternsValidity.java
+++ b/test/jdk/java/text/Format/CompactNumberFormat/TestCompactPatternsValidity.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,7 +22,7 @@
  */
 /*
  * @test
- * @bug 8177552
+ * @bug 8177552 8217254
  * @summary Checks the validity of compact number patterns specified through
  *          CompactNumberFormat constructor
  * @run testng/othervm TestCompactPatternsValidity
@@ -80,7 +80,9 @@
             // A non empty pattern containing no 0s (min integer digits)
             {new String[]{"K", "0K", "00K"}},
             // 0s (min integer digits) exceeding for the range at index 3
-            {new String[]{"", "", "0K", "00000K"}},};
+            {new String[]{"", "", "0K", "00000K"}},
+            // null as a compact pattern
+            {new String[]{"", "", null, "00K"}},};
     }
 
     @DataProvider(name = "validPatternsFormat")
@@ -124,7 +126,7 @@
     }
 
     @Test(dataProvider = "invalidPatterns",
-            expectedExceptions = RuntimeException.class)
+            expectedExceptions = IllegalArgumentException.class)
     public void testInvalidCompactPatterns(String[] compactPatterns) {
         new CompactNumberFormat("#,##0.0#", DecimalFormatSymbols
                 .getInstance(Locale.US), compactPatterns);