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 when the code parameter is null — a guard preventing an empty/null source file from being written to the work directory before path and IO handling.
Source
Thrown at spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/utils/FileUtils.java:37
import java.io.IOException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
/**
* @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
- Check code != null before calling writeCodeToFile.
- Default the code to an empty string if null is acceptable for your use case.
- Trace upstream: fix the producer (LLM node, parser) that returned null.
- Log/return a clear domain error instead of writing the file when code is missing.
Example fix
// before
FileUtils.writeCodeToFile(workDir, filename, llmCode); // NPE-style IAE when llmCode == null
// after
if (llmCode == null) {
throw new IllegalStateException("Model returned no code for " + filename);
}
FileUtils.writeCodeToFile(workDir, filename, llmCode); Defensive patterns
Strategy: validation
Validate before calling
if (code == null || code.isBlank()) {
throw new IllegalArgumentException("code must be a non-blank string before calling FileUtils.writeCodeToFile");
} Try / catch
try {
FileUtils.writeCodeToFile(workDir, filename, code);
} catch (IllegalArgumentException e) {
logger.error("Refusing to write {}: {}", filename, e.getMessage());
// fall back: skip write or surface a domain error
} Prevention
- Null-check model/LLM outputs before persisting them to files.
- Default null code to a sentinel or empty string only if blank files are acceptable.
- Add an upstream assertion where code is produced.
- Fail fast at the producer rather than the file layer.
When it happens
Trigger: Calling FileUtils.writeCodeToFile(workDir, filename, null) — typically when the code string came from an LLM response or an upstream variable that was never populated.
Common situations: A code-generation node produced no output, a null response from the model was passed through unguarded, or a workflow variable defaulted to null.
Related errors
- ${type} requires valid configuration
- Version ID cannot be null
- Configuration cannot be null
- Skill path cannot be null or empty
- Code must not be null
AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09).
Data as JSON: /api/errors/59f1cf0fe6e25066.
Report an issue: GitHub.