users@jaxb.java.net

NPE with Messages.format

From: Dan Diephouse <dan_at_envoisolutions.com>
Date: Mon, 14 Nov 2005 09:33:56 -0500

The Messages.format method depends on classloaders implementing
Class.getPackage(), which isn't always the case (it can return null, see
the javadocs). So while building you can get an exception like:

java.lang.NullPointerException
        at com.sun.tools.xjc.Messages.format(Messages.java:33)
        at com.sun.tools.xjc.Driver.getBuildID(Driver.java:351)
        at com.sun.tools.xjc.XJC2Task.execute(XJC2Task.java:308)
        at org.apache.tools.ant.Task.perform(Task.java:341)

I've attached a patch which remedies the problem. Cheers,
- Dan

PS - Is someone still going to upload the JAXB jars?

-- 
Dan Diephouse
Envoi Solutions LLC
http://netzooid.com


Index: Messages.java
===================================================================
RCS file: /cvs/jaxb2-sources/jaxb-ri/xjc/src/com/sun/tools/xjc/Messages.java,v
retrieving revision 1.7
diff -u -r1.7 Messages.java
--- Messages.java 19 Sep 2005 22:11:04 -0000 1.7
+++ Messages.java 14 Nov 2005 14:29:41 -0000
@@ -30,8 +30,17 @@
 {
     /** Loads a string resource and formats it with specified arguments. */
     static String format( String property, Object... args ) {
- String text = ResourceBundle.getBundle(Messages.class.getPackage().getName() +".MessageBundle").getString(property);
+ String pckg = getPackageName(Messages.class.getName());
+ String text = ResourceBundle.getBundle(pckg +".MessageBundle").getString(property);
+
         return MessageFormat.format(text,args);
+ }
+
+ static String getPackageName(String clsName) {
+ int idx = clsName.lastIndexOf(".");
+ if (idx == -1) idx = 0;
+
+ return clsName.substring(0, idx);
     }
     
 //