Details
Description
MessageFormat uses java.lang.Class.getPackage() to obtain the name of the package for the 'reference class' in order to choose an appropriate resource bundle. Unfortunately, java.lang.Class.getPackage() does not guarantee to return a Package object.
It may return 'null' if (a) the reference class is loaded by the primordial classloader, or (b) if the classloader didn't create a package object and associated it with the class.
I am seeing this problem while running unit tests of my application within Maven. Example stacktrace follows:
java.lang.ExceptionInInitializerError
at org.apache.hivemind.impl.RegistryImpl.getServicePoint(RegistryImpl.java:142)
at org.apache.hivemind.impl.RegistryImpl.getService(RegistryImpl.java:149)
at org.apache.hivemind.impl.RegistryImpl.startup(RegistryImpl.java:321)
at org.apache.hivemind.impl.RegistryBuilder.constructRegistry(RegistryBuilder.java:417)
... lots more
Caused by: java.lang.NullPointerException
at org.apache.hivemind.impl.MessageFormatter.<init>(MessageFormatter.java:49)
at org.apache.hivemind.impl.MessageFormatter.<init>(MessageFormatter.java:44)
at org.apache.hivemind.impl.ImplMessages.<clinit>(ImplMessages.java:45)
... 59 more
The only safe way that I can see to achieve the same effect as Class.getPackage().getName() is to do something like:
String lReferenceClassName = referenceClass.getName();
String lPackageName = lReferenceClassName.substring(0,lReferenceClassName.lastIndexOf('.'));
... probably as a private function within MessageFormat itself.