{"record":{"id":"9323f1313d4b6cdb","repo":"apache/dolphinscheduler","slug":"taskinstanceid-cannot-be-null","errorCode":null,"errorMessage":"taskInstanceId cannot be null","messagePattern":"taskInstanceId cannot be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/repository/impl/TaskInstanceContextDaoImpl.java","lineNumber":61,"sourceCode":"            TaskInstanceContextDao {\n\n    public TaskInstanceContextDaoImpl(TaskInstanceContextMapper taskInstanceContextMapper) {\n        super(taskInstanceContextMapper);\n    }\n\n    @Override\n    public List<TaskInstanceContext> queryListByTaskInstanceIdAndContextType(Integer taskInstanceId,\n                                                                             ContextType contextType) {\n        if (taskInstanceId == null) {\n            return Collections.emptyList();\n        }\n        return mybatisMapper.queryListByTaskInstanceIdAndContextType(taskInstanceId, contextType);\n    }\n\n    @Override\n    public int deleteByTaskInstanceIdAndContextType(Integer taskInstanceId, ContextType contextType) {\n        if (taskInstanceId == null) {\n            throw new IllegalArgumentException(\"taskInstanceId cannot be null\");\n        }\n        return mybatisMapper.deleteByTaskInstanceIdAndContextType(taskInstanceId, contextType);\n    }\n\n    @Override\n    public int upsertTaskInstanceContext(TaskInstanceContext taskInstanceContext) {\n        if (taskInstanceContext == null) {\n            return 0;\n        }\n        TaskInstanceContext dbTaskInstanceContext =\n                mybatisMapper.queryListByTaskInstanceIdAndContextType(taskInstanceContext.getTaskInstanceId(),\n                        taskInstanceContext.getContextType()).stream().findFirst().orElse(null);\n        if (dbTaskInstanceContext == null) {\n            return mybatisMapper.insert(taskInstanceContext);\n        } else {\n            List<AbstractTaskInstanceContext> dbDependentResultTaskInstanceContextList =\n                    dbTaskInstanceContext.getTaskInstanceContext();\n            dbDependentResultTaskInstanceContextList.addAll(taskInstanceContext.getTaskInstanceContext());","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/apache/dolphinscheduler/blob/02eac45a1b6676e639fcbfb4be2243de5771b05d/dolphinscheduler-dao/src/main/java/org/apache/dolphinscheduler/dao/repository/impl/TaskInstanceContextDaoImpl.java#L43-L79","documentation":"TaskInstanceContextDaoImpl.deleteByTaskInstanceIdAndContextType removes context rows for a given task instance and context type. The task instance id is the primary lookup key, so a null value is rejected with IllegalArgumentException before the mapper executes.","triggerScenarios":"Calling deleteByTaskInstanceIdAndContextType(null, someContextType), generally when the TaskInstance object feeding the id was null or its id was not yet generated (task not persisted).","commonSituations":"Cleanup logic after task failure that references a task instance never persisted; callers passing taskInstance.getId() on a newly constructed (unsaved) instance.","solutions":["Ensure the task instance is persisted and has a valid id before deleting its contexts.","Null-check the TaskInstance/id at the call site and skip deletion when absent.","Verify the ordering: fetch the task instance from the DB first, then clean up contexts."],"exampleFix":"// before\ntaskInstanceContextDao.deleteByTaskInstanceIdAndContextType(taskInstance.getId(), ContextType.CACHE);\n// after\nif (taskInstance != null && taskInstance.getId() != null) {\n    taskInstanceContextDao.deleteByTaskInstanceIdAndContextType(taskInstance.getId(), ContextType.CACHE);\n}","handlingStrategy":"validation","validationCode":"if (taskInstance == null || taskInstance.getId() == null) {\n    return;\n}\ntaskInstanceContextDao.deleteByTaskInstanceIdAndContextType(taskInstance.getId(), contextType);","typeGuard":"boolean hasValidId(TaskInstance t) { return t != null && t.getId() != null; }","tryCatchPattern":"try {\n    dao.deleteByTaskInstanceIdAndContextType(id, contextType);\n} catch (IllegalArgumentException e) {\n    log.error(\"taskInstanceId is required for context deletion\", e);\n}","preventionTips":["Only operate on persisted task instances with generated ids.","Null-check the TaskInstance before using its id for DAO calls.","Order cleanup steps after the instance is confirmed in the DB."],"tags":["null-check","dao","task-instance-context"],"backgroundTag":"null-argument","analyzedSha":"02eac45a1b6676e639fcbfb4be2243de5771b05d","analyzedAt":"2026-09-06T17:43:00.555Z","contentChangedAt":"2026-09-06T17:43:00.555Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}