{"record":{"id":"002c500cf0f7151b","repo":"gradle/gradle","slug":"could-not-create-service-of-s-using-s-s","errorCode":null,"errorMessage":"Could not create service of %s using %s.%s().","messagePattern":"Could not create service of (.+?) using (.+?)\\.(.+?)\\(\\)\\.","errorType":"exception","errorClass":"ServiceCreationException","httpStatus":null,"severity":"error","filePath":"platforms/core-runtime/service-registry-impl/src/main/java/org/gradle/internal/service/DefaultServiceRegistry.java","lineNumber":909,"sourceCode":"        }\n\n        @Override\n        protected String getFactoryDisplayName() {\n            return String.format(\"method %s.%s()\", format(getMethod().getOwner()), getMethod().getName());\n        }\n\n        @Override\n        protected Object invokeMethod(Object[] params) {\n            if (target == null) {\n                throw new IllegalStateException(\"The target of the factory method has been discarded after the first service creation attempt\");\n            }\n\n            Object result;\n            ServiceMethod method = getMethod();\n            try {\n                result = method.invoke(target, params);\n            } catch (Exception e) {\n                throw new ServiceCreationException(String.format(\"Could not create service of %s using %s.%s().\",\n                    format(\"type\", serviceTypes),\n                    method.getOwner().getSimpleName(),\n                    method.getName()),\n                    e);\n            }\n\n            if (result == null) {\n                throw new ServiceCreationException(String.format(\"Could not create service of %s using %s.%s() as this method returned null.\",\n                    format(\"type\", serviceTypes),\n                    method.getOwner().getSimpleName(),\n                    method.getName()));\n            }\n            return result;\n        }\n\n        @Override\n        protected Object createServiceInstance() {\n            Object result = super.createServiceInstance();","sourceCodeStart":891,"sourceCodeEnd":927,"githubUrl":"https://github.com/gradle/gradle/blob/534f27719b66953f95cc907aae7f2c1b12f5482d/platforms/core-runtime/service-registry-impl/src/main/java/org/gradle/internal/service/DefaultServiceRegistry.java#L891-L927","documentation":"A service produced by a factory method (a create*() method on a provider added via addProvider, or an explicit factory) failed because invoking the method threw; the registry wraps the original exception in ServiceCreationException and reports the service type plus the factory's owner class and method name. The cause chain carries the real error, so the factory method body, not the registry wiring, is where the failure happened.","triggerScenarios":"registry.get(Foo.class) where Foo is produced by, for example, Foo createFoo() on a provider object, and the method throws an NPE, IllegalStateException or similar because configuration is unset, the environment is missing something, or the factory body has a bug.","commonSituations":"Factory methods dereferencing state initialized later or conditionally absent (config flags, env vars, files); factories calling external systems that are down; refactorings that made a dependency nullable.","solutions":["Inspect the cause of the ServiceCreationException and fix what the factory method body threw (usually an NPE from missing setup)","Make the factory defensive: validate its inputs at the top and throw a descriptive exception naming the missing piece","If the failure is environmental, resolve the environment or configuration before the service is first requested"],"exampleFix":"// before\npublic Cache createCache() { return new DefaultCache(settings.dir()); } // NPE when settings unset\n\n// after\npublic Cache createCache() {\n    if (settings == null) {\n        throw new IllegalStateException(\"settings not registered before Cache creation\");\n    }\n    return new DefaultCache(settings.dir());\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    return registry.get(Cache.class);\n} catch (ServiceCreationException e) {\n    if (e.getMessage() != null && e.getMessage().contains(\"Could not create service of\") && e.getCause() != null) {\n        throw new IllegalStateException(\"Cache factory failed: \" + e.getCause().getMessage(), e.getCause());\n    }\n    throw e;\n}","preventionTips":["Validate factory-method inputs at the top of the method and throw descriptive errors instead of failing mid-body","Avoid environmental lookups (files, env vars, network) inside factory methods; pass them in as registered services","Always log the cause chain: the registry wraps it, so the root is one getCause() away"],"tags":["gradle","service-registry","factory-method","dependency-injection"],"backgroundTag":"service-factory-method-threw","analyzedSha":"534f27719b66953f95cc907aae7f2c1b12f5482d","analyzedAt":"2026-08-22T08:09:12.375Z","schemaVersion":2},"datasetVersion":"2026-08-22T09:17:25.309Z"}