ResourceBundle.Control getFallbackLocale(String, Locale) in Java

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

Syntax:

Locale java.util.ResourceBundle.Control.getFallbackLocale(String baseName, Locale locale)

This method takes two arguments. This method returns a Locale to be used as a fallback locale for further resource bundle searches by the ResourceBundle.getBundle factory method.

Parameters: Two parameters are required for this method.

baseName: the base name of the resource bundle, a fully qualified class name for which ResourceBundle.getBundle has been unable to find any resource bundles (except for the base bundle).

locale: the Locale for which ResourceBundle.getBundle has been unable to find any resource bundles (except for the base bundle).

Returns: a Locale for the fallback search, or null if no further fallback search is desired.

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 ResourceBundleControlgetFallbackLocale {
    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.getFallbackLocale(
baseName, locale));
    }
}

Output:

null


Approach 2: NullPointerException

Java 

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

public class ResourceBundleControlgetFallbackLocale {
    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.getFallbackLocale(
baseName, locale));
    }
}

Output:

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


No comments:

Post a Comment