* @stable ICU 2.0
*/
public synchronized void setPositivePrefix(String prefix) {
+ if (prefix == null) {
+ throw new NullPointerException();
+ }
properties.setPositivePrefix(prefix);
refreshFormatter();
}
* <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();
}
* @stable ICU 2.0
*/
public synchronized void setPositiveSuffix(String suffix) {
+ if (suffix == null) {
+ throw new NullPointerException();
+ }
properties.setPositiveSuffix(suffix);
refreshFormatter();
}
* @stable ICU 2.0
*/
public synchronized void setNegativeSuffix(String suffix) {
+ if (suffix == null) {
+ throw new NullPointerException();
+ }
properties.setNegativeSuffix(suffix);
refreshFormatter();
}
* @see PatternString#parseToExistingProperties
*/
void setPropertiesFromPattern(String pattern, int ignoreRounding) {
+ if (pattern == null) {
+ throw new NullPointerException();
+ }
PatternString.parseToExistingProperties(pattern, properties, ignoreRounding);
}
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;
}
}
+ @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