{"record":{"id":"59f1cf0fe6e25066","repo":"alibaba/spring-ai-alibaba","slug":"code-must-not-be-null","errorCode":null,"errorMessage":"Code must not be null","messagePattern":"Code must not be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/utils/FileUtils.java","lineNumber":37,"sourceCode":"import java.io.IOException;\nimport java.net.URL;\nimport java.nio.file.Files;\nimport java.nio.file.Path;\n\n/**\n * @author HeYQ\n * @since 2024-11-28 11:47\n */\npublic class FileUtils {\n\n\tprivate FileUtils() {\n\t\tthrow new IllegalStateException(\"Utility class\");\n\t}\n\n\tpublic static void writeCodeToFile(String workDir, String filename, String code) {\n\t\ttry {\n\t\t\tif (code == null) {\n\t\t\t\tthrow new IllegalArgumentException(\"Code must not be null\");\n\t\t\t}\n\t\t\tPath filepath = Path.of(workDir, filename);\n\t\t\t// ensure the parent directory exists\n\t\t\tPath fileDir = filepath.getParent();\n\t\t\tif (fileDir != null && !Files.exists(fileDir)) {\n\t\t\t\tFiles.createDirectories(fileDir);\n\t\t\t}\n\t\t\t// write the code to the file\n\t\t\tFiles.writeString(filepath, code);\n\t\t}\n\t\tcatch (IOException e) {\n\t\t\tthrow new RuntimeException(e);\n\t\t}\n\t}\n\n\t/**\n\t * Deletes the file specified by the filename from the provided working directory.\n\t * @param workDir The working directory where the file to be deleted is located.","sourceCodeStart":19,"sourceCodeEnd":55,"githubUrl":"https://github.com/alibaba/spring-ai-alibaba/blob/f82da0b50f35744c13968191be2b1cd2452ef550/spring-ai-alibaba-graph-core/src/main/java/com/alibaba/cloud/ai/graph/utils/FileUtils.java#L19-L55","documentation":"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.","triggerScenarios":"Calling FileUtils.writeCodeToFile(workDir, filename, null) — typically when the code string came from an LLM response or an upstream variable that was never populated.","commonSituations":"A code-generation node produced no output, a null response from the model was passed through unguarded, or a workflow variable defaulted to null.","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."],"exampleFix":"// before\nFileUtils.writeCodeToFile(workDir, filename, llmCode); // NPE-style IAE when llmCode == null\n// after\nif (llmCode == null) {\n    throw new IllegalStateException(\"Model returned no code for \" + filename);\n}\nFileUtils.writeCodeToFile(workDir, filename, llmCode);","handlingStrategy":"validation","validationCode":"if (code == null || code.isBlank()) {\n    throw new IllegalArgumentException(\"code must be a non-blank string before calling FileUtils.writeCodeToFile\");\n}","typeGuard":null,"tryCatchPattern":"try {\n    FileUtils.writeCodeToFile(workDir, filename, code);\n} catch (IllegalArgumentException e) {\n    logger.error(\"Refusing to write {}: {}\", filename, e.getMessage());\n    // fall back: skip write or surface a domain error\n}","preventionTips":["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."],"tags":["file-write","null-check","validation"],"backgroundTag":"null-argument","analyzedSha":"f82da0b50f35744c13968191be2b1cd2452ef550","analyzedAt":"2026-09-09T15:32:42.421Z","contentChangedAt":"2026-09-09T15:32:42.421Z","schemaVersion":2},"datasetVersion":"2026-09-14T11:17:12.474Z"}