alibaba/spring-ai-alibaba · error · IllegalArgumentException
Code must not be null
Error message
Code must not be null
What it means
FileUtils.writeCodeToFile throws IllegalArgumentException('Code must not be null') when the code parameter is null, before writing the file. It ensures a valid source string exists prior to creating directories and writing the temp code file.
Source
Thrown at spring-boot-starters/spring-ai-alibaba-starter-builtin-nodes/src/main/java/com/alibaba/cloud/ai/graph/utils/FileUtils.java:43
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Collections;
import java.util.Enumeration;
/**
* @author HeYQ
* @since 2024-11-28 11:47
*/
public class FileUtils {
private FileUtils() {
throw new IllegalStateException("Utility class");
}
public static void writeCodeToFile(String workDir, String filename, String code) {
try {
if (code == null) {
throw new IllegalArgumentException("Code must not be null");
}
Path filepath = Path.of(workDir, filename);
// ensure the parent directory exists
Path fileDir = filepath.getParent();
if (fileDir != null && !Files.exists(fileDir)) {
Files.createDirectories(fileDir);
}
// write the code to the file
Files.writeString(filepath, code);
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Deletes the file specified by the filename from the provided working directory.
* @param workDir The working directory where the file to be deleted is located.View on GitHub (pinned to f82da0b50f)
Solutions
- Validate code is non-null (and ideally non-blank) before calling writeCodeToFile.
- Trace upstream why the code block is empty — check LLM response parsing and node input mappings.
- Substitute a default or skip execution gracefully when no code is present.
- Catch IllegalArgumentException and surface a clear error to the workflow.
Example fix
// before
FileUtils.writeCodeToFile(workDir, filename, block.getCode()); // throws if null
// after
String code = block.getCode();
if (code == null || code.isBlank()) {
throw new IllegalStateException("Code block has no body");
}
FileUtils.writeCodeToFile(workDir, filename, code); Defensive patterns
Strategy: validation
Validate before calling
if (code == null || code.isBlank()) {
throw new IllegalArgumentException("Cannot write code file: code is null or blank");
} Type guard
boolean hasCodeBody(String code) {
return code != null && !code.isBlank();
} Try / catch
try {
FileUtils.writeCodeToFile(workDir, filename, code);
} catch (IllegalArgumentException e) {
log.error("No code body to write — check upstream node output", e);
throw e;
} Prevention
- Validate code blocks right after parsing the LLM response
- Use Optional or explicit defaults so null code never propagates to file writes
- Fail fast at the workflow input boundary when a code block is empty
When it happens
Trigger: Calling writeCodeToFile(workDir, filename, null) — e.g. the code block was never populated from the LLM response, or a config map lookup returned null for the code key.
Common situations: LLM response parse yielded no code body; upstream null propagation from an empty Optional/map.get; wiring a code executor node without setting its input mapping.
Related errors
- item cannot be null
- Code must not be null
- Agent name cannot be null or empty
- Either language or code must be provided.
- oss object name is invalid
AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09).
Data as JSON: /api/errors/a62eaec29b57d3ac.
Report an issue: GitHub.