alibaba/nacos · error · NullPointerException
httpClientFactory is null
Error message
httpClientFactory is null
What it means
Thrown by HttpClientBeanHolder.getNacosRestTemplate(HttpClientFactory) as a NullPointerException when the httpClientFactory argument is null. This is a simple precondition check before the factory is used to look up or create a singleton NacosRestTemplate. The holder caches templates by factory class name in a static map with double-checked locking.
Source
Thrown at common/src/main/java/com/alibaba/nacos/common/http/HttpClientBeanHolder.java:56
private static final Map<String, NacosRestTemplate> SINGLETON_REST = new HashMap<>(10);
private static final Map<String, NacosAsyncRestTemplate> SINGLETON_ASYNC_REST =
new HashMap<>(10);
private static final AtomicBoolean ALREADY_SHUTDOWN = new AtomicBoolean(false);
static {
ThreadUtils.addShutdownHook(HttpClientBeanHolder::shutdown);
}
public static NacosRestTemplate getNacosRestTemplate(Logger logger) {
return getNacosRestTemplate(new DefaultHttpClientFactory(logger));
}
public static NacosRestTemplate getNacosRestTemplate(HttpClientFactory httpClientFactory) {
if (httpClientFactory == null) {
throw new NullPointerException("httpClientFactory is null");
}
String factoryName = httpClientFactory.getClass().getName();
NacosRestTemplate nacosRestTemplate = SINGLETON_REST.get(factoryName);
if (nacosRestTemplate == null) {
synchronized (SINGLETON_REST) {
nacosRestTemplate = SINGLETON_REST.get(factoryName);
if (nacosRestTemplate != null) {
return nacosRestTemplate;
}
nacosRestTemplate = httpClientFactory.createNacosRestTemplate();
SINGLETON_REST.put(factoryName, nacosRestTemplate);
}
}
return nacosRestTemplate;
}
public static NacosAsyncRestTemplate getNacosAsyncRestTemplate(Logger logger) {
return getNacosAsyncRestTemplate(new DefaultHttpClientFactory(logger));View on GitHub (pinned to 9b989acdf1)
Solutions
- Ensure the HttpClientFactory argument is non-null before calling getNacosRestTemplate.
- Use the convenience overload getNacosRestTemplate(Logger) if you just need a default factory.
- If using dependency injection, verify the HttpClientFactory bean is properly configured.
Example fix
// before
NacosRestTemplate template = HttpClientBeanHolder.getNacosRestTemplate(maybeNullFactory);
// after — use convenience overload or null-check
NacosRestTemplate template = HttpClientBeanHolder.getNacosRestTemplate(logger);
// or
if (factory != null) {
NacosRestTemplate template = HttpClientBeanHolder.getNacosRestTemplate(factory);
} Defensive patterns
Strategy: validation
Validate before calling
if (httpClientFactory == null) {
throw new IllegalArgumentException("httpClientFactory must not be null");
}
NacosRestTemplate template = HttpClientBeanHolder.getNacosRestTemplate(httpClientFactory); Try / catch
try {
template = HttpClientBeanHolder.getNacosRestTemplate(factory);
} catch (NullPointerException e) {
if (e.getMessage() != null && e.getMessage().contains("httpClientFactory is null")) {
template = HttpClientBeanHolder.getNacosRestTemplate(logger);
} else {
throw e;
}
} Prevention
- Never pass a null HttpClientFactory to the explicit overload.
- Prefer the convenience overload getNacosRestTemplate(Logger) when a default factory suffices.
- Add null-checks in calling code before invoking the holder.
When it happens
Trigger: Calling getNacosRestTemplate((HttpClientFactory) null) directly. The convenience overload getNacosRestTemplate(Logger) internally creates a DefaultHttpClientFactory, so this only triggers when the explicit factory overload is used with null.
Common situations: Custom code that conditionally creates an HttpClientFactory and passes null when the condition is not met; DI frameworks injecting null when the bean is missing; test code that manually invokes the holder.
Related errors
- Agent replacement must not be null
- Agent publish request must not be null
- AgentResourceExt must not be null
- {fieldName} must not be null
- 400
AI-assisted analysis of alibaba/nacos@9b989acdf1 (2026-08-14).
Data as JSON: /api/errors/c6c681216a0f1d91.
Report an issue: GitHub.