ResourceBundle.getBundle(String, Locale): This method is available in java.util.ResourceBundle class of Java.
Syntax:
ResourceBundle java.util.ResourceBundle.getBundle(String baseName, Locale locale)
This method takes two arguments. This method gets a resource bundle using the specified base name and locale, and the caller module.
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 is desired.
Returns: a resource bundle for the given base name and locale.
Throws:
1. NullPointerException - if baseName or locale is null.
2. MissingResourceException - if no resource bundle for the specified base name can be found.
Approach 1: When no exception
Java
import java.util.Locale;import java.util.ResourceBundle;public class ResourceBundlegetBundle3 {public static void main(String[] args) {String baseName = "Hello";System.out.println(ResourceBundle.getBundle(baseName,Locale.US));}}
Output:
java.util.PropertyResourceBundle@3567135c
Approach 2: NullPointerException
Java
import java.util.Locale;import java.util.ResourceBundle;public class ResourceBundlegetBundle3 {public static void main(String[] args) {String baseName = null;System.out.println(ResourceBundle.getBundle(baseName,Locale.US));}}
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:932)
No comments:
Post a Comment