ResourceBundle.Control toBundleName(String, Locale) in Java

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

Syntax:

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

This method takes two arguments. This method converts the given baseName and locale to the bundle name.

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 should be loaded.

Returns: the bundle name for the resource bundle.

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 ResourceBundleControltoBundleName {
    public static void main(String[] args) {

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

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

    }
}

Output:

Hello_en_US


Approach 2: NullPointerException 

Java

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

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

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

        String baseName = null;
        System.out.println(reControl.toBundleName(
baseName, Locale.US));

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "str" is null at java.base/java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:105) at java.base/java.lang.StringBuilder.<init>(StringBuilder.java:125) at java.base/java.util.ResourceBundle$Control.toBundleName(ResourceBundle.java:3459)


No comments:

Post a Comment