chinabugotech/hutool · error · NullPointerException

File path is blank!

Error message

File path is blank!

What it means

FileUtil.file(File parent, String path) builds a File under a parent and applies a zip-slip security check. If path is null, empty, or whitespace-only it throws NullPointerException("File path is blank!"). Note the library deliberately uses NPE rather than IllegalArgumentException for a blank string argument.

Source

Thrown at hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java:318

	 * @param path   文件路径
	 * @return File
	 */
	public static File file(String parent, String path) {
		return file(new File(parent), path);
	}

	/**
	 * 创建File对象<br>
	 * 根据的路径构建文件,在Win下直接构建,在Linux下拆分路径单独构建
	 * 此方法会检查slip漏洞,漏洞说明见http://blog.nsfocus.net/zip-slip-2/
	 *
	 * @param parent 父文件对象
	 * @param path   文件路径
	 * @return File
	 */
	public static File file(File parent, String path) {
		if (StrUtil.isBlank(path)) {
			throw new NullPointerException("File path is blank!");
		}
		return checkSlip(parent, buildFile(parent, path));
	}

	/**
	 * 通过多层目录参数创建文件<br>
	 * 此方法会检查slip漏洞,漏洞说明见http://blog.nsfocus.net/zip-slip-2/
	 *
	 * @param directory 父目录
	 * @param names     元素名(多层目录名),由外到内依次传入
	 * @return the file 文件
	 * @since 4.0.6
	 */
	public static File file(File directory, String... names) {
		Assert.notNull(directory, "directory must not be null");
		if (ArrayUtil.isEmpty(names)) {
			return directory;
		}

View on GitHub (pinned to 8870454b2a)

Solutions

  1. Validate with StrUtil.isNotBlank(path) before calling file().
  2. Provide a sensible default or fail early with your own error when the path source is empty.
  3. Sanitize path segments from split operations to drop blanks.

Example fix

// before
File f = FileUtil.file(parent, name); // NPE if name is blank

// after
if (StrUtil.isBlank(name)) {
    throw new IllegalArgumentException("name required");
}
File f = FileUtil.file(parent, name);
Defensive patterns

Strategy: validation

Validate before calling

if (StrUtil.isBlank(path)) {
    throw new IllegalArgumentException("path must not be blank");
}
File f = FileUtil.file(parent, path);

Type guard

static boolean isUsablePath(String path) {
    return path != null && !path.trim().isEmpty();
}

Try / catch

try {
    return FileUtil.file(parent, path);
} catch (NullPointerException e) {
    // blank path: provide a clear error with the parent context
}

Prevention

When it happens

Trigger: Calling FileUtil.file(parent, path) where path is null, "", or contains only whitespace.

Common situations: Building paths from missing config keys; splitting a path into segments where one segment is empty; a Map.get/getOrDefault returning null that is passed straight through.

Related errors


AI-assisted analysis of chinabugotech/hutool@8870454b2a (2026-08-14). Data as JSON: /api/errors/dc1ccf1114cc493f. Report an issue: GitHub.