{"record":{"id":"a752346541d1eae8","repo":"flowable/flowable-engine","slug":"null-value-cannot-be-saved-a75234","errorCode":null,"errorMessage":"null value cannot be saved","messagePattern":"null value cannot be saved","errorType":"validation","errorClass":"ActivitiIllegalArgumentException","httpStatus":null,"severity":"error","filePath":"modules/flowable5-engine/src/main/java/org/activiti/engine/impl/variable/JPAEntityMappings.java","lineNumber":71,"sourceCode":"    }\n\n    public EntityMetaData getEntityMetaData(Class<?> clazz) {\n        EntityMetaData metaData = classMetaDatamap.get(clazz.getName());\n        if (metaData == null) {\n            // Class not present in meta-data map, create metaData for it and add\n            metaData = scanClass(clazz);\n            classMetaDatamap.put(clazz.getName(), metaData);\n        }\n        return metaData;\n    }\n\n    private EntityMetaData scanClass(Class<?> clazz) {\n        return enitityScanner.scanClass(clazz);\n    }\n\n    public String getJPAClassString(Object value) {\n        if (value == null) {\n            throw new ActivitiIllegalArgumentException(\"null value cannot be saved\");\n        }\n\n        EntityMetaData metaData = getEntityMetaData(value.getClass());\n        if (!metaData.isJPAEntity()) {\n            throw new ActivitiIllegalArgumentException(\"Object is not a JPA Entity: class='\" + value.getClass() + \"', \" + value);\n        }\n\n        // Extract the class from the Entity instance\n        return metaData.getEntityClass().getName();\n    }\n\n    public String getJPAIdString(Object value) {\n        EntityMetaData metaData = getEntityMetaData(value.getClass());\n        if (!metaData.isJPAEntity()) {\n            throw new ActivitiIllegalArgumentException(\"Object is not a JPA Entity: class='\" + value.getClass() + \"', \" + value);\n        }\n        Object idValue = getIdValue(value, metaData);\n        return getIdString(idValue);","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/flowable/flowable-engine/blob/d6d39ce1c69ff244f2d9dc6af756a9b95e865586/modules/flowable5-engine/src/main/java/org/activiti/engine/impl/variable/JPAEntityMappings.java#L53-L89","documentation":"JPAEntityMappings serializes JPA entity variables for persistence. getJPAClassString extracts the entity class name for storage; it refuses a null value because a null cannot carry JPA metadata and cannot be persisted as a JPA entity variable.","triggerScenarios":"Calling getJPAClassString(null) directly, or storing a null variable through the JPA entity variable type mapping (e.g. taskService.setVariable / runtimeService.setVariable resolved to the JPA type) with a null value.","commonSituations":"A process variable set from an expression or delegate field that evaluates to null (entity not yet loaded/found); passing a result of em.find() that returned null; spring bean method returning null wired as a variable.","solutions":["Ensure the value is a non-null JPA entity before setting the variable","Guard the caller: if the entity is null, set the variable to null explicitly via a null-typed variable instead of the JPA mapping, or remove the variable","Fix the upstream fetch so the entity is loaded before it reaches variable serialization"],"exampleFix":"// before\nruntimeService.setVariable(executionId, \"order\", possiblyNullOrder);\n// after\nif (possiblyNullOrder != null) {\n    runtimeService.setVariable(executionId, \"order\", possiblyNullOrder);\n} else {\n    runtimeService.setVariable(executionId, \"order\", null);\n}","handlingStrategy":"validation","validationCode":"if (value == null) {\n    throw new IllegalArgumentException(\"Cannot store null as JPA entity variable\");\n}\nruntimeService.setVariable(id, \"order\", value);","typeGuard":"boolean isNonNull(Object v) { return v != null; }","tryCatchPattern":"try {\n    runtimeService.setVariable(id, \"order\", value);\n} catch (ActivitiIllegalArgumentException e) {\n    if (e.getMessage().contains(\"null value cannot be saved\")) {\n        runtimeService.setVariable(id, \"order\", null);\n    } else { throw e; }\n}","preventionTips":["Null-check entities before assigning process variables","Avoid setting variables from expressions that can evaluate to null","Log entity fetch results so null returns are caught early"],"tags":["jpa","null-value","process-variables"],"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"}