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

  1. Check code != null before calling writeCodeToFile.
  2. Default the code to an empty string if null is acceptable for your use case.
  3. Trace upstream: fix the producer (LLM node, parser) that returned null.
  4. 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

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


AI-assisted analysis of alibaba/spring-ai-alibaba@f82da0b50f (2026-09-09). Data as JSON: /api/errors/59f1cf0fe6e25066. Report an issue: GitHub.