{"record":{"id":"154c630807830ab5","repo":"ssssssss-team/spider-flow","slug":"file-is-null","errorCode":null,"errorMessage":"file is null","messagePattern":"file is null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"spider-flow-api/src/main/java/org/spiderflow/io/RandomAccessFileReader.java","lineNumber":35,"sourceCode":"\tprivate long index;\n\n\t/**\n\t * 读取顺序，默认倒叙\n\t */\n\tprivate boolean reversed;\n\n\t/**\n\t * 缓冲区大小\n\t */\n\tprivate int bufSize;\n\n\tpublic RandomAccessFileReader(RandomAccessFile raf, long index, boolean reversed) throws IOException {\n\t\tthis(raf, index, 1024, reversed);\n\t}\n\n\tpublic RandomAccessFileReader(RandomAccessFile raf, long index, int bufSize, boolean reversed) throws IOException {\n\t\tif (raf == null) {\n\t\t\tthrow new NullPointerException(\"file is null\");\n\t\t}\n\t\tthis.raf = raf;\n\t\tthis.reversed = reversed;\n\t\tthis.bufSize = bufSize;\n\t\tthis.index = index;\n\t\tthis.init();\n\t}\n\n\tprivate void init() throws IOException {\n\t\tif (reversed) {\n\t\t\tthis.index = this.index == -1 ? this.raf.length() : Math.min(this.index, this.raf.length());\n\t\t} else {\n\t\t\tthis.index = Math.min(Math.max(this.index, 0), this.raf.length());\n\t\t}\n\t\tif (this.index > 0) {\n\t\t\tthis.raf.seek(this.index);\n\t\t}\n\t}","sourceCodeStart":17,"sourceCodeEnd":53,"githubUrl":"https://github.com/ssssssss-team/spider-flow/blob/c799cca99c7d064673dc79e6ef06a08bbc0e292d/spider-flow-api/src/main/java/org/spiderflow/io/RandomAccessFileReader.java#L17-L53","documentation":"RandomAccessFileReader's constructor validates its RandomAccessFile argument and throws NullPointerException('file is null') when raf is null. The reader cannot function without an underlying file handle, so null is rejected up front with init() following.","triggerScenarios":"Calling `new RandomAccessFileReader(null, index, reversed)` or the 4-arg constructor with a null raf, typically when a prior RandomAccessFile creation failed or a variable was never assigned.","commonSituations":"File that failed to open (swallowed exception left raf null), path configuration pointing to a non-existent file, DI/spring wiring missing, refactors that pass a nullable field straight into the reader.","solutions":["Ensure the RandomAccessFile is successfully created before constructing the reader (e.g. `new RandomAccessFile(path, \"r\")`), and check for null.","Null-check the caller's file variable and fail fast with a clearer message about which path failed.","Wrap file opening so an IOException opening the file surfaces instead of propagating a null handle."],"exampleFix":"// before\nRandomAccessFileReader r = new RandomAccessFileReader(raf, 0, true);\n// after\nObjects.requireNonNull(raf, \"raf must be opened for path \" + path);\nRandomAccessFileReader r = new RandomAccessFileReader(raf, 0, true);","handlingStrategy":"validation","validationCode":"if (raf == null) { throw new IllegalArgumentException(\"raf must be opened before creating RandomAccessFileReader\"); }\nRandomAccessFileReader reader = new RandomAccessFileReader(raf, 0, true);","typeGuard":"java.util.Objects.requireNonNull(raf, \"raf is null\");","tryCatchPattern":"try (RandomAccessFile raf = new RandomAccessFile(path, \"r\")) {\n  RandomAccessFileReader reader = new RandomAccessFileReader(raf, 0, true);\n  // use reader\n}","preventionTips":["Open the RandomAccessFile in try-with-resources immediately before constructing the reader.","Never store raf in a nullable field without a null check at use.","Let FileNotFoundException from opening propagate rather than swallowing it and leaving raf null."],"tags":["java","null-pointer","io","constructor"],"backgroundTag":"null-argument","analyzedSha":"c799cca99c7d064673dc79e6ef06a08bbc0e292d","analyzedAt":"2026-09-08T13:47:08.225Z","contentChangedAt":"2026-09-08T13:47:08.225Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}