{"record":{"id":"4d8f2acc2dd80f50","repo":"flowable/flowable-engine","slug":"cannot-set-tenant-context-to-null","errorCode":null,"errorMessage":"Cannot set tenant context to null","messagePattern":"Cannot set tenant context to null","errorType":"exception","errorClass":"FlowableException","httpStatus":null,"severity":"error","filePath":"modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/tenant/CurrentTenant.java","lineNumber":31,"sourceCode":"package org.flowable.common.engine.impl.tenant;\n\nimport org.flowable.common.engine.api.FlowableException;\nimport org.flowable.common.engine.api.tenant.TenantContext;\n\n/**\n * @author Filip Hrisafov\n */\npublic abstract class CurrentTenant {\n\n    private static TenantContext tenantContext = new ThreadLocalTenantContext();\n\n    public static TenantContext getTenantContext() {\n        return tenantContext;\n    }\n\n    public static void setTenantContext(TenantContext tenantContext) {\n        if (tenantContext == null) {\n            throw new FlowableException(\"Cannot set tenant context to null\");\n        }\n        CurrentTenant.tenantContext = tenantContext;\n    }\n\n}\n","sourceCodeStart":13,"sourceCodeEnd":37,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/tenant/CurrentTenant.java#L13-L37","documentation":"CurrentTenant.setTenantContext throws FlowableException if the given TenantContext is null. The engine holds a static TenantContext used for tenant resolution; replacing it with null would break every subsequent tenant lookup, so it is disallowed.","triggerScenarios":"Calling CurrentTenant.setTenantContext(null), typically during engine shutdown/customization, or a factory method returning null that is passed directly into the setter.","commonSituations":"Spring/config bean for TenantContext not yet initialized (null injected); custom TenantContext factory returning null on misconfiguration.","solutions":["Construct or obtain a valid TenantContext implementation before setting it","Check why the provider returned null (missing bean/config) and fix initialization order","Use getTenantContext to keep the existing context if you intended no change"],"exampleFix":"// before\ncurrentTenant.setTenantContext(resolveTenantContext());\n// after\nTenantContext ctx = resolveTenantContext();\nif (ctx != null) {\n    currentTenant.setTenantContext(ctx);\n}","handlingStrategy":"type-guard","validationCode":"TenantContext ctx = tenantContextProvider.get();\nif (ctx == null) {\n    throw new IllegalStateException(\"TenantContext provider returned null; check engine configuration\");\n}\nCurrentTenant.setTenantContext(ctx);","typeGuard":"boolean isValidTenantContext(TenantContext ctx) {\n    return ctx != null;\n}","tryCatchPattern":"try {\n    CurrentTenant.setTenantContext(ctx);\n} catch (FlowableException e) {\n    logger.error(\"TenantContext setup failed: {}\", e.getMessage());\n}","preventionTips":["Ensure the TenantContext bean is initialized before engine use","Never pass factory results straight into setTenantContext without a null check","Use Spring's ObjectProvider/getIfAvailable to detect missing beans explicitly"],"tags":["flowable","tenant-context","null-check","engine-config"],"backgroundTag":"null-argument","analyzedSha":"d6d39ce1c69ff244f2d9dc6af756a9b95e865586","analyzedAt":"2026-09-11T06:41:19.413Z","contentChangedAt":"2026-09-11T06:41:19.413Z","schemaVersion":2},"datasetVersion":"2026-09-18T11:17:12.947Z"}