We have a case where a PC running a Java 8 application using 1.8.0_092 is returning null from Locale.getDefault()
. While dealing with this is trivial, I'm more worried that null is being returned, since the documentation for the method does not indicate that null is possible. We are not setting a locale from the java command line, and are not calling setDefault()
anywhere.
What might be wrong in this installation that Locale.getDefault()
would return null? Am I not reading Java documentation correctly?
Looking at source code of JDK 1.8.0_092 I see that the value returned by Locale.getDefault()
comes the field defaultLocale
(Locale.java:2022).
private volatile static Locale defaultLocale = initDefault();
The initDefault()
builds a key (LocaleKey
) based on the variables language
, region
, script
, country
and variant
, then search into a Cache
object which is LocaleObjectCache<LocaleKey, Locale>
.
Well, I don't want bother you with other details but in essence, this code is looking in a Map for an existing LocaleKey
for the given environment so it is possible that such Locale does not exist.