ResourceBundle.Control toResourceName(String, String) in Java

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

Syntax:

String java.util.ResourceBundle.Control.toResourceName(String bundleName, String suffix)

This method takes two arguments. This method converts the given bundleName to the form required by the ClassLoader.getResourcemethod by replacing all occurrences of '.' in bundleName with '/' and appending a '.' and the given file suffix.

Parameters: Two parameters are required for this method.

bundleName: the bundle name suffix the file type suffix.

Returns: the converted resource name.

Throws:

NullPointerException - if bundleName or suffix is null

Approach 1: When no exception

Java

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

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

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

        String bundleName = "Hello", suffix = "lo";
        System.out.println(reControl.toResourceName(
bundleName, suffix));

    }
}

Output:

Hello.lo


Approach 2: NullPointerException

Java

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

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

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

        String bundleName = null, suffix = "lo";
        System.out.println(reControl.toResourceName(
bundleName, suffix));

    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "bundleName" is null at java.base/java.util.ResourceBundle$Control.toResourceName(ResourceBundle.java:3503)


No comments:

Post a Comment