{"record":{"id":"16de64d6bec5cb7a","repo":"flowable/flowable-engine","slug":"cannot-initialize-byte-array-there-is-no-command","errorCode":null,"errorMessage":"Cannot initialize byte array. There is no command context and there is no command Executor","messagePattern":"Cannot initialize byte array\\. There is no command context and there is no command Executor","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/persistence/entity/ByteArrayRef.java","lineNumber":148,"sourceCode":"                getEngineConfiguration(engineType).getByteArrayEntityManager().delete(entity);\n            } else {\n                getEngineConfiguration(engineType).getByteArrayEntityManager().deleteByteArrayById(id);\n            }\n            entity = null;\n            id = null;\n            deleted = true;\n        }\n    }\n\n    protected void ensureInitialized(String engineType) {\n        if (id != null && entity == null) {\n            CommandContext commandContext = Context.getCommandContext();\n            if (commandContext != null) {\n                entity = getEngineConfiguration(engineType).getByteArrayEntityManager().findById(id);\n            } else if (commandExecutor != null) {\n                entity = commandExecutor.execute(context -> getEngineConfiguration(engineType).getByteArrayEntityManager().findById(id));\n            } else {\n                throw new IllegalStateException(\"Cannot initialize byte array. There is no command context and there is no command Executor\");\n            }\n\n            if (entity != null) {\n                name = entity.getName();\n            }\n        }\n    }\n\n    public boolean isDeleted() {\n        return deleted;\n    }\n\n    /**\n     * This makes a copy of this {@link ByteArrayRef}: a new\n     * {@link ByteArrayRef} instance will be created, however with the same id,\n     * name and {@link ByteArrayEntity} instances.\n     */\n    public ByteArrayRef copy() {","sourceCodeStart":130,"sourceCodeEnd":166,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable-engine-common/src/main/java/org/flowable/common/engine/impl/persistence/entity/ByteArrayRef.java#L130-L166","documentation":"ByteArrayRef lazily loads its byte array entity, which requires either an active CommandContext or a configured CommandExecutor. ensureInitialized throws IllegalStateException when both are absent, since there is no way to run the database lookup for the referenced byte array.","triggerScenarios":"Calling getBytes()/setBytes()/getEntity() on a ByteArrayRef outside of a command (e.g. in a plain thread, after the engine closed, or in a standalone app) when neither Context.getCommandContext() nor a commandExecutor is available.","commonSituations":"Reading content (e.g. an attachment or variable of type bytes) after the command/transaction ended; using engine entities in custom code without running inside a ProcessEngine command or setting a commandExecutor; unit tests without a command context.","solutions":["Access the byte content inside a command, e.g. managementService.executeCommand(commandContext -> byteArrayRef.getBytes())","Keep the entity/content access within the same command/transaction where the engine is active","If running standalone, configure the ByteArrayRef's commandExecutor before use"],"exampleFix":"// before\nbyte[] data = byteArrayRef.getBytes(); // outside command context\n// after\nbyte[] data = managementService.executeCommand(ctx -> byteArrayRef.getBytes());","handlingStrategy":"try-catch","validationCode":"boolean safeToRead = org.flowable.common.engine.impl.context.Context.getCommandContext() != null;","typeGuard":null,"tryCatchPattern":"try {\n    byte[] data = ref.getBytes();\n} catch (IllegalStateException e) {\n    if (e.getMessage().startsWith(\"Cannot initialize byte array\")) {\n        // re-run inside executeCommand\n    }\n    throw e;\n}","preventionTips":["Only read byte-array-backed content inside commands/transactions","Use managementService.executeCommand for out-of-command access","Don't cache ByteArrayRef objects beyond the command lifetime"],"tags":["command-context","byte-array","lifecycle"],"backgroundTag":"missing-command-context","analyzedSha":"d6d39ce1c69ff244f2d9dc6af756a9b95e865586","analyzedAt":"2026-09-11T06:41:19.413Z","contentChangedAt":"2026-09-11T06:41:19.413Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}