Currency getSymbol(Locale) in Java

getSymbol(Locale): This method is available in java.util.Currency class of Java.

Syntax:

String java.util.Currency.getSymbol(Locale locale)

This method takes one argument of type Locale as its parameter. This method gets the symbol of this currency for the specified locale.

Parameters: One parameter is required for this method.

locale: the locale for which a display name for this currency is needed.

Returns: the symbol of this currency for the specified locale.

Throws:

NullPointerException - if the locale is null.

Approach 1: When no exception

Java

import java.util.Currency;
import java.util.Locale;

public class CurrencygetSymbol2 {
    public static void main(String[] args) {

        Currency currency = Currency.getInstance(Locale.US);

        System.out.println(currency.getSymbol(Locale.US));

    }
}

Output:

$


Approach 2: NullPointerException

Java

import java.util.Currency;
import java.util.Locale;

public class CurrencygetSymbol2 {
    public static void main(String[] args) {

        Currency currency = Currency.getInstance(Locale.US);

        System.out.println(currency.getSymbol(null));

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.Locale.getUnicodeLocaleType(String)" because "l" is null at java.base/sun.util.locale.provider.CalendarDataUtility.findRegionOverride(CalendarDataUtility.java:136) at java.base/java.util.Currency.getSymbol(Currency.java:545)


No comments:

Post a Comment