apache/hadoop · error · NullPointerException
Source must not be null
Error message
Source must not be null
What it means
First guard clause of Storage.nativeCopyFileUnbuffered: it throws NullPointerException before any I/O happens when srcFile is null. The method copies a large file (typically a fsimage/edits segment) using OS-specific unbuffered IO, and its contract requires a non-null regular-file source.
Source
Thrown at hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/common/Storage.java:1327
* <strong>Note:</strong> Setting <code>preserveFileDate</code> to
* {@code true} tries to preserve the file's last modified
* date/times using {@link File#setLastModified(long)}, however it is
* not guaranteed that the operation will succeed.
* If the modification operation fails, no indication is provided.
*
* @param srcFile an existing file to copy, must not be {@code null}
* @param destFile the new file, must not be {@code null}
* @param preserveFileDate true if the file date of the copy
* should be the same as the original
*
* @throws NullPointerException if source or destination is {@code null}
* @throws IOException if source or destination is invalid
* @throws IOException if an IO error occurs during copying
*/
public static void nativeCopyFileUnbuffered(File srcFile, File destFile,
boolean preserveFileDate) throws IOException {
if (srcFile == null) {
throw new NullPointerException("Source must not be null");
}
if (destFile == null) {
throw new NullPointerException("Destination must not be null");
}
if (srcFile.exists() == false) {
throw new FileNotFoundException("Source '" + srcFile + "' does not exist");
}
if (srcFile.isDirectory()) {
throw new IOException("Source '" + srcFile + "' exists but is a directory");
}
if (srcFile.getCanonicalPath().equals(destFile.getCanonicalPath())) {
throw new IOException("Source '" + srcFile + "' and destination '" +
destFile + "' are the same");
}
File parentFile = destFile.getParentFile();
if (parentFile != null) {
if (!parentFile.mkdirs() && !parentFile.isDirectory()) {
throw new IOException("Destination '" + parentFileView on GitHub (pinned to 2add963021)
Solutions
- Pass a non-null srcFile
- Fail fast at the call site with an actionable message: Objects.requireNonNull(srcFile, "srcFile")
- Fix the upstream lookup that produced null (e.g. validate the config key exists before building the File)
Example fix
// before Storage.nativeCopyFileUnbuffered(resolveSrc(cfg), dst, true); // resolveSrc returns null // after File src = Objects.requireNonNull(resolveSrc(cfg), "checkpoint src missing from config"); Storage.nativeCopyFileUnbuffered(src, dst, true);
Defensive patterns
Strategy: validation
Validate before calling
Objects.requireNonNull(srcFile, "srcFile must be resolved before copy"); if (!srcFile.isFile()) throw new FileNotFoundException(srcFile.toString());
Type guard
static boolean isCopyableSource(File f) {
return f != null && f.isFile();
} Prevention
- Annotate nullable returns in path-resolution code and check them immediately
- Fail fast with Objects.requireNonNull at API boundaries instead of relying on the library's NPE
- Validate config-derived paths once at startup, not per call
When it happens
Trigger: Calling nativeCopyFileUnbuffered with a null File, usually because the caller resolved the source path from a nullable lookup (missing config property, absent checkpoint file) and forwarded it unchecked.
Common situations: Unit tests invoking the helper directly; code that maps a configuration value to a File and skips the missing-property case; refactors where a field initialization was dropped.
Related errors
- Destination must not be null
- The filePath should not be null!
- Block cannot be null
- Provided block and location cannot be null
- Source '{srcFile}' does not exist
AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22).
Data as JSON: /api/errors/e522782da8f69ef7.
Report an issue: GitHub.