ssssssss-team/spider-flow · error · NullPointerException

file is null

Error message

file is null

What it means

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.

Solutions

  1. Ensure the RandomAccessFile is successfully created before constructing the reader (e.g. `new RandomAccessFile(path, "r")`), and check for null.
  2. Null-check the caller's file variable and fail fast with a clearer message about which path failed.
  3. Wrap file opening so an IOException opening the file surfaces instead of propagating a null handle.

Example fix

// before
RandomAccessFileReader r = new RandomAccessFileReader(raf, 0, true);
// after
Objects.requireNonNull(raf, "raf must be opened for path " + path);
RandomAccessFileReader r = new RandomAccessFileReader(raf, 0, true);
Defensive patterns

Strategy: validation

Validate before calling

if (raf == null) { throw new IllegalArgumentException("raf must be opened before creating RandomAccessFileReader"); }
RandomAccessFileReader reader = new RandomAccessFileReader(raf, 0, true);

Type guard

java.util.Objects.requireNonNull(raf, "raf is null");

Try / catch

try (RandomAccessFile raf = new RandomAccessFile(path, "r")) {
  RandomAccessFileReader reader = new RandomAccessFileReader(raf, 0, true);
  // use reader
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of ssssssss-team/spider-flow@c799cca99c (2026-09-08). Data as JSON: /api/errors/154c630807830ab5. Report an issue: GitHub.

Appendix: source

Thrown at spider-flow-api/src/main/java/org/spiderflow/io/RandomAccessFileReader.java:35

	private long index;

	/**
	 * 读取顺序,默认倒叙
	 */
	private boolean reversed;

	/**
	 * 缓冲区大小
	 */
	private int bufSize;

	public RandomAccessFileReader(RandomAccessFile raf, long index, boolean reversed) throws IOException {
		this(raf, index, 1024, reversed);
	}

	public RandomAccessFileReader(RandomAccessFile raf, long index, int bufSize, boolean reversed) throws IOException {
		if (raf == null) {
			throw new NullPointerException("file is null");
		}
		this.raf = raf;
		this.reversed = reversed;
		this.bufSize = bufSize;
		this.index = index;
		this.init();
	}

	private void init() throws IOException {
		if (reversed) {
			this.index = this.index == -1 ? this.raf.length() : Math.min(this.index, this.raf.length());
		} else {
			this.index = Math.min(Math.max(this.index, 0), this.raf.length());
		}
		if (this.index > 0) {
			this.raf.seek(this.index);
		}
	}

View on GitHub (pinned to c799cca99c)