ResourceBundle.Control getControl(List) in Java

getControl(List<String>): This method is available in java.util.ResourceBundle.Control class of Java.

Syntax:

Control java.util.ResourceBundle.Control.getControl(List<String> formats)

This method takes one argument. This method returns a ResourceBundle.Control in which the getFormats method returns the specified formats.

Note: The formats must be equal to one of Control.FORMAT_PROPERTIES, Control.FORMAT_CLASS or Control.FORMAT_DEFAULT.

Parameters: One parameter is required for this method.

formats: the formats to be returned by the ResourceBundle.Control.getFormats method.

Returns: a ResourceBundle.Control supporting the specified formats.

Throws:

1. NullPointerException - if the format is null.

2. IllegalArgumentException - if formats is unknown

Approach 1: When no exception

Java

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

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

        List<String> formats = new ArrayList<String>();
        formats.add("java.properties");

        Control str = Control.getControl(formats);
        System.out.println(str.toBundleName("Hello", Locale.US));
    }
}

Output:

Hello_en_US


Approach 2: NullPointerException 

Java

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

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

        List<String> formats = null;

        Control str = Control.getControl(formats);
        System.out.println(str.toBundleName("Hello", Locale.US));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.util.List.equals(Object)" because "formats" is null at java.base/java.util.ResourceBundle$Control.getControl(ResourceBundle.java:2607)



Approach 3: IllegalArgumentException

Java

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

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

        List<String> formats = new ArrayList<String>();
        formats.add("properties");

        Control str = Control.getControl(formats);
        System.out.println(str.toBundleName("Hello", Locale.US));
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException at java.base/java.util.ResourceBundle$Control.getControl(ResourceBundle.java:2616)


No comments:

Post a Comment