ResourceBundle.getBundle(String, Control): This method is available in java.util.ResourceBundle class of Java.
Syntax:
ResourceBundle java.util.ResourceBundle.getBundle(String baseName, Control control)
This method takes two arguments. This method returns a resource bundle using the specified base name, the default locale, and the specified control.
Parameters: Two parameters are required for this method.
baseName: the base name of the resource bundle, a fully qualified classname.
control: the control which gives information for the resource bundle loading process.
Returns: a resource bundle for the given base name and the default locale.
Throws:
1. NullPointerException - if baseName or control is null.
2. MissingResourceException - if no resource bundle for the specified base name can be found.
3. llegalArgumentException - if the given control doesn't perform properly.
4. UnsupportedOperationException - if this method is called in a named module.
Note: Please create a Hello_en_US.properties in the classpath with content
Hello = Hello Java Program
Approach 1: When no exception
Java
import java.util.ResourceBundle;import java.util.ResourceBundle.Control;public class ResourceBundlegetBundle2 {public static void main(String[] args) {String baseName = "Hello";ResourceBundle.Control control =ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);ResourceBundle bundle =ResourceBundle.getBundle(baseName, control);System.out.println(bundle.getString(baseName));}}
Output:
Hello Java Program
Approach 2: NullPointerException
Java
import java.util.ResourceBundle;import java.util.ResourceBundle.Control;public class ResourceBundlegetBundle2 {public static void main(String[] args) {String baseName = null;ResourceBundle.Control control =ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);ResourceBundle bundle =ResourceBundle.getBundle(baseName, control);System.out.println(bundle.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.getBundleImpl(ResourceBundle.java:1556) at java.base/java.util.ResourceBundle.getBundle(ResourceBundle.java:904)
Approach 3: MissingResourceException
Java
import java.util.ResourceBundle;import java.util.ResourceBundle.Control;public class ResourceBundlegetBundle2 {public static void main(String[] args) {String baseName = "hello";ResourceBundle.Control control =ResourceBundle.Control.getControl(Control.FORMAT_DEFAULT);ResourceBundle bundle =ResourceBundle.getBundle(baseName, control);System.out.println(bundle.getString(baseName));}}
Output:
Exception in thread "main" java.util.MissingResourceException: Can't find resource for bundle java.util.PropertyResourceBundle, key hello at java.base/java.util.ResourceBundle.getObject(ResourceBundle.java:564) at java.base/java.util.ResourceBundle.getString(ResourceBundle.java:521)
No comments:
Post a Comment