Skip to content

NullPointerException thrown when MessageSourceSupport#formatMessage is invoked with a null Locale #34751

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
GiovanniChien opened this issue Apr 14, 2025 · 1 comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: invalid An issue that we don't feel is valid

Comments

@GiovanniChien
Copy link

GiovanniChien commented Apr 14, 2025

If you use org.springframework.context.MessageSource#getMessage(java.lang.String, java.lang.Object[], java.lang.String, java.util.Locale) and locale is null. When the org.springframework.context.support.MessageSourceSupport#formatMessage method is executed, the ConcurrentHashMap is used as the cache and the locale is null. As a result, the null pointer is generated when use null as the key.

protected String formatMessage(String msg, @Nullable Object[] args, Locale locale) {
	if (!isAlwaysUseMessageFormat() && ObjectUtils.isEmpty(args)) {
		return msg;
	}
	Map<Locale, MessageFormat> messageFormatsPerLocale = this.messageFormatsPerMessage
			.computeIfAbsent(msg, key -> new ConcurrentHashMap<>());

        // NullPointerException thrown when locale is null
	MessageFormat messageFormat = messageFormatsPerLocale.computeIfAbsent(locale, key -> {
		try {
			return createMessageFormat(msg, locale);
		}
		catch (IllegalArgumentException ex) {
			// Invalid message format - probably not intended for formatting,
			// rather using a message structure with no arguments involved...
			if (isAlwaysUseMessageFormat()) {
				throw ex;
			}
			// Silently proceed with raw message if format not enforced...
			return INVALID_MESSAGE_FORMAT;
		}
	});
	if (messageFormat == INVALID_MESSAGE_FORMAT) {
		return msg;
	}
	synchronized (messageFormat) {
		return messageFormat.format(resolveArguments(args, locale));
	}
}
@GiovanniChien GiovanniChien changed the title NullPointerException thrown when MessageSourceSupport#formatMessage is invoked when locale is null NullPointerException thrown when MessageSourceSupport#formatMessage is invoked when locale is null Apr 14, 2025
@GiovanniChien GiovanniChien changed the title NullPointerException thrown when MessageSourceSupport#formatMessage is invoked when locale is null NullPointerException thrown when MessageSourceSupport#formatMessage is invoked when locale is null Apr 14, 2025
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Apr 14, 2025
@sbrannen sbrannen added the in: core Issues in core modules (aop, beans, core, context, expression) label Apr 14, 2025
@sbrannen
Copy link
Member

That is the expected behavior.

The Locale parameter is not annotated with @Nullable.

Thus, a null locale is not supported.

In light of that, I am closing this issue as "works as expected".

@sbrannen sbrannen closed this as not planned Won't fix, can't repro, duplicate, stale Apr 14, 2025
@sbrannen sbrannen added status: invalid An issue that we don't feel is valid and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Apr 14, 2025
@sbrannen sbrannen changed the title NullPointerException thrown when MessageSourceSupport#formatMessage is invoked when locale is null NullPointerException thrown when MessageSourceSupport#formatMessage is invoked with a null Locale Apr 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: invalid An issue that we don't feel is valid
Projects
None yet
Development

No branches or pull requests

3 participants