Skip to content
Advertisement

Why do I get MissingResourceException

Why does this throw a MissingResourceException ? Am I mistaken or is that not the point of ListResourceBundles is to provide the resources in code?

public class Hello extends Thread {

    public static void main(String[] args) {

         ResourceBundle resBundle = ResourceBundle.getBundle("ResourceBundle", new Locale("it", "IT", ""));
         System.out.println(resBundle.getObject(new Integer(1).toString()));
    }


}

in a seperate file:

public class ResourceBundle_it_IT extends ListResourceBundle {

    public Object[][] getContents() {
        return contents;
    }
    static final Object[][] contents = {
        {"1", "Uno"},
        {"2", "Duo"},
        {"3", "Trie"},};

}

My Error:

 run:
Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name ResourceBundle, locale it_IT
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1564)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1387)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:845)
    at hello.Hello.main(Hello.java:21)
/home/maxbisesi/.cache/netbeans/8.1/executor-snippets/run.xml:53: Java returned: 1
BUILD FAILED (total time: 0 seconds)

Advertisement

Answer

When you get an error, and try to understand the reason, the first step is always to read the stack trace of the error. In this case, the stack trace is

Exception in thread "main" java.util.MissingResourceException: Can't find bundle for base name ResourceBundle, locale it_IT
    at java.util.ResourceBundle.throwMissingResourceException(ResourceBundle.java:1564)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1387)
    at java.util.ResourceBundle.getBundle(ResourceBundle.java:845)
    at Hello.main(Hello.java:9)
Caused by: java.lang.IllegalAccessException: Class java.util.ResourceBundle$Control can not access a member of class ResourceBundle_it_IT with modifiers ""
    at sun.reflect.Reflection.ensureMemberAccess(Reflection.java:102)
    at java.lang.Class.newInstance(Class.java:436)
    at java.util.ResourceBundle$Control.newBundle(ResourceBundle.java:2645)
    at java.util.ResourceBundle.loadBundle(ResourceBundle.java:1501)
    at java.util.ResourceBundle.findBundle(ResourceBundle.java:1465)
    at java.util.ResourceBundle.getBundleImpl(ResourceBundle.java:1361)
    ... 2 more

And if you read it, the problem becomes obvious:

Class java.util.ResourceBundle$Control can not access a member of class ResourceBundle_it_IT with modifiers “”

Move the class ResourceBundle_it_IT to its onwn file, where it belongs. make it public as the error message suggests, and the problem disappears.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement