{"record":{"id":"c6c681216a0f1d91","repo":"alibaba/nacos","slug":"httpclientfactory-is-null","errorCode":null,"errorMessage":"httpClientFactory is null","messagePattern":"httpClientFactory is null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"common/src/main/java/com/alibaba/nacos/common/http/HttpClientBeanHolder.java","lineNumber":56,"sourceCode":"    \n    private static final Map<String, NacosRestTemplate> SINGLETON_REST = new HashMap<>(10);\n    \n    private static final Map<String, NacosAsyncRestTemplate> SINGLETON_ASYNC_REST =\n        new HashMap<>(10);\n    \n    private static final AtomicBoolean ALREADY_SHUTDOWN = new AtomicBoolean(false);\n    \n    static {\n        ThreadUtils.addShutdownHook(HttpClientBeanHolder::shutdown);\n    }\n    \n    public static NacosRestTemplate getNacosRestTemplate(Logger logger) {\n        return getNacosRestTemplate(new DefaultHttpClientFactory(logger));\n    }\n    \n    public static NacosRestTemplate getNacosRestTemplate(HttpClientFactory httpClientFactory) {\n        if (httpClientFactory == null) {\n            throw new NullPointerException(\"httpClientFactory is null\");\n        }\n        String factoryName = httpClientFactory.getClass().getName();\n        NacosRestTemplate nacosRestTemplate = SINGLETON_REST.get(factoryName);\n        if (nacosRestTemplate == null) {\n            synchronized (SINGLETON_REST) {\n                nacosRestTemplate = SINGLETON_REST.get(factoryName);\n                if (nacosRestTemplate != null) {\n                    return nacosRestTemplate;\n                }\n                nacosRestTemplate = httpClientFactory.createNacosRestTemplate();\n                SINGLETON_REST.put(factoryName, nacosRestTemplate);\n            }\n        }\n        return nacosRestTemplate;\n    }\n    \n    public static NacosAsyncRestTemplate getNacosAsyncRestTemplate(Logger logger) {\n        return getNacosAsyncRestTemplate(new DefaultHttpClientFactory(logger));","sourceCodeStart":38,"sourceCodeEnd":74,"githubUrl":"https://github.com/alibaba/nacos/blob/9b989acdf181d00898f2e8839257bb2b2a3cefe3/common/src/main/java/com/alibaba/nacos/common/http/HttpClientBeanHolder.java#L38-L74","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nNacosRestTemplate template = HttpClientBeanHolder.getNacosRestTemplate(maybeNullFactory);\n\n// after — use convenience overload or null-check\nNacosRestTemplate template = HttpClientBeanHolder.getNacosRestTemplate(logger);\n// or\nif (factory != null) {\n    NacosRestTemplate template = HttpClientBeanHolder.getNacosRestTemplate(factory);\n}","handlingStrategy":"validation","validationCode":"if (httpClientFactory == null) {\n    throw new IllegalArgumentException(\"httpClientFactory must not be null\");\n}\nNacosRestTemplate template = HttpClientBeanHolder.getNacosRestTemplate(httpClientFactory);","typeGuard":null,"tryCatchPattern":"try {\n    template = HttpClientBeanHolder.getNacosRestTemplate(factory);\n} catch (NullPointerException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"httpClientFactory is null\")) {\n        template = HttpClientBeanHolder.getNacosRestTemplate(logger);\n    } else {\n        throw e;\n    }\n}","preventionTips":["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."],"tags":["http-client","null-check","validation","internal-api"],"backgroundTag":null,"analyzedSha":"9b989acdf181d00898f2e8839257bb2b2a3cefe3","analyzedAt":"2026-08-14T07:17:31.569Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}