-
Bug
-
Resolution: Cannot Reproduce
-
P3
-
None
-
1.4.1
-
x86
-
windows_2000
Name: nt126004 Date: 04/11/2003
FULL PRODUCT VERSION :
C:\Java\jdk1.3.1_06\bin>java -version
java version "1.3.1_06"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.1_06-b01)
Java HotSpot(TM) Client VM (build 1.3.1_06-b01, mixed mode)
also seems to happen in the LATEST JDK (ours is the following)
C:\>java -version
java version "1.4.1_02"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.1_02-b06)
Java HotSpot(TM) Client VM (build 1.4.1_02-b06, mixed mode)
though I have not browsed its code (but assume the problem remains)
FULL OS VERSION :
C:\>ver
Microsoft Windows 2000 [Version 5.00.2195]
A DESCRIPTION OF THE PROBLEM :
When a Locale is created with at least one parameter (in the constructor) being null (instead of empty) a NullPointerException occurs, that is not documented (and therefore not expectable, though obvious if one looks at the Locale classe's code ;)
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Just try to create a Locale (constructor or any method calling the internal toUpperCase or toLowerCase methods) giving any parameter (e.g. language) a null String instead of an empty one ("").
EXPECTED VERSUS ACTUAL BEHAVIOR :
If I e.g. create a Locale with
new Locale(null, "AT", null)
I'd still expect to get a valid locale (with empty or null language and variant and an "AT" country) and no Exception.
A NullPointerException (that is not documented in the API/JavaDoc) resulting in the error message below (by J2EE container)
ERROR MESSAGES/STACK TRACES THAT OCCUR :
500 Internal Server Error
java.lang.NullPointerException
at java.util.Locale.toLowerCase(Locale.java:1174)
at java.util.Locale.convertOldISOCodes(Locale.java:1214)
at java.util.Locale.<init>(Locale.java:264)
at java.util.Locale.<init>(Locale.java:280)
at cc.diamonddogs.redact.actions.RedactBaseAction.getLocale(RedactBaseAction.java:33)
at cc.diamonddogs.redact.actions.RedactBaseAction.getTexts(RedactBaseAction.java:38)
at cc.diamonddogs.redact.actions.RedactBaseAction.getText(RedactBaseAction.java:50)
at cc.diamonddogs.redact.actions.Search.doExecute(Search.java:177)
at webwork.action.ActionSupport.execute(ActionSupport.java:87)
at cc.diamonddogs.redact.actions.RedactBaseAction.execute(RedactBaseAction.java:148)
at webwork.dispatcher.ServletDispatcher.service(ServletDispatcher.java:184)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:336)
at com.evermind[Orion/1.5.2 (build 10460)]._eab.doFilter(Unknown Source)
at com.opensymphony.module.sitemesh.filter.PageFilter.parsePage(Unknown Source)
at com.opensymphony.module.sitemesh.filter.PageFilter.doFilter(Unknown Source)
at com.evermind[Orion/1.5.2 (build 10460)]._cxb._abe(Unknown Source)
at com.evermind[Orion/1.5.2 (build 10460)]._cxb._uec(Unknown Source)
at com.evermind[Orion/1.5.2 (build 10460)]._io._twc(Unknown Source)
at com.evermind[Orion/1.5.2 (build 10460)]._io._gc(Unknown Source)
at com.evermind[Orion/1.5.2 (build 10460)]._if.run(Unknown Source)
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
This is a method from a Servlet class (WebWork Action to be more precise)
where the language is taken from the session and under some circumstances the "lang" String could be null instead of an empty ("")
public Locale getLocale() {
String lang = getLang();
return new Locale(lang, "AT"); /** THIS is where Locale throws an exception */
}
The Locale's own method that causes the exception is the following:
/*
* Locale needs its own, locale insenitive version of toLowerCase to
* avoid circularity problems between Locale and String.
* The most straightforward algorithm is used. Look at optimizations later.
*/
private String toLowerCase(String str) {
char[] buf = str.toCharArray();
for (int i = 0; i < buf.length; i++) {
buf[i] = Character.toLowerCase( buf[i] );
}
return new String( buf );
}
// here is a very simple test case.
import java.util.Locale;
public class Test {
public static void main(String[] args) {
Locale l = new Locale(null, null);
}
}
(maybe those "optimizations" could include null-checking ?;)
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
Checking all parameters for null that are not constant strings before constructing a Locale with them
If the Strings given as parameters MUST NOT BE null, so PLEASE DOCUMENT that more clearly in the API doc, or solve it inside Locale itself (whatever seems more appropriate to you)
Something like
* @throws NullPointerException if <code>language</code>, <code>country</code> or <code>variant</code> is null
would be the least you should add to the important methods (like the constructor) so people know what to expect if they do so...
(Review ID: 183813)
======================================================================