ResourceBundle.Control getCandidateLocales(String, Locale) in Java

getCandidateLocales(String, Locale): This method is available in java.util.ResourceBundle.Control class of Java.

Syntax:

List<Locale> java.util.ResourceBundle.Control.getCandidateLocales(String baseName, Locale locale)

This method takes two arguments. This method returns a List of Locales as candidate locales for baseName and locale.

Parameters: Two parameters are required for this method.

baseName: the base name of the resource bundle, a fully qualified class name.

locale: the locale for which a resource bundle is desired.

Returns: List of candidate Locales for the given locale.

Throws:

NullPointerException - if baseName or locale is null

Approach 1: When no exception

Java

import java.util.Locale;
import java.util.ResourceBundle;
import java.util.ResourceBundle.Control;

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

        ResourceBundle.Control reControl =
ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);

        String baseName = "Hello";
        Locale locale = Locale.US;
        System.out.println(reControl.getCandidateLocales(
baseName, locale));
    }
}

Output:

[en_US, en, ]


Approach 2: NullPointerException

Java

import java.util.Locale;
import java.util.ResourceBundle;
import java.util.ResourceBundle.Control;

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

        ResourceBundle.Control reControl =
ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);

        String baseName = null;
        Locale locale = Locale.US;
        System.out.println(reControl.getCandidateLocales(
baseName, locale));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.ResourceBundle$Control.getCandidateLocales(ResourceBundle.java:2875)


No comments:

Post a Comment