ResourceBundle.getBundle(String, Locale, ClassLoader, Control) in Java

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

Syntax:

ResourceBundle java.util.ResourceBundle.getBundle(String baseName, Locale targetLocale, ClassLoader loader, Control control)

This method takes four arguments. This method returns a resource bundle using the specified base name, targetlocale, classloader, and control.

Parameters: Four parameters are required for this method.

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

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

loader: the class loader from which to load the resource bundle.

control: the control which gives information for the resource bundle loading process.

Returns: a resource bundle for the given base name and locale.

Throws:

1. NullPointerException - if baseName, targetLocale, loader, or control is null.

2. MissingResourceException - if no resource bundle for the specified base name can be found.

3. IllegalArgumentException - if the given control doesn't perform properly.

4. UnsupportedOperationException - if this method is called in a named module.

Approach 1: When no exception

Java

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

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

        String baseName = "Hello";

        ResourceBundle.Control rbc =
ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);
        ClassLoader cl = ClassLoader.getSystemClassLoader();

        System.out.println(ResourceBundle.getBundle(baseName,
Locale.US, cl, rbc).getString(baseName));
    }
}

Output:

Hello Java Program


Approach 2: NullPointerException 

Java

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

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

        String baseName = null;

        ResourceBundle.Control rbc =
ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);
        ClassLoader cl = ClassLoader.getSystemClassLoader();

        System.out.println(ResourceBundle.getBundle(baseName,
Locale.US, cl, rbc).getString(baseName));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.hashCode()" because "this.name" is null at java.base/java.util.ResourceBundle$CacheKey.hashCode(ResourceBundle.java:744) at java.base/java.util.concurrent.ConcurrentHashMap.get(ConcurrentHashMap.java:936) at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1629) at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1593) at java.base/java.util.ResourceBundle.getBundle(ResourceBundle.java:1509)



Approach 3: MissingResourceException

Java

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

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

        String baseName = "hello";

        ResourceBundle.Control rbc =
ResourceBundle.Control.getControl(Control.FORMAT_CLASS);
        ClassLoader cl = ClassLoader.getSystemClassLoader();

        System.out.println(ResourceBundle.getBundle(baseName,
Locale.US, cl, rbc));
    }
}

Output:

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name hello, locale en_US at java.base/java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:2055) at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1689) at java.base/java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1593) at java.base/java.util.ResourceBundle.getBundle(ResourceBundle.java:1509)


No comments:

Post a Comment