{"record":{"id":"27a64bcddb9ddf80","repo":"chinabugotech/hutool","slug":"file-uri-is-null","errorCode":null,"errorMessage":"File uri is null!","messagePattern":"File uri is null!","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java","lineNumber":380,"sourceCode":"\t\tfor (String name : names) {\n\t\t\tif (file == null) {\n\t\t\t\tfile = file(name);\n\t\t\t} else {\n\t\t\t\tfile = file(file, name);\n\t\t\t}\n\t\t}\n\t\treturn file;\n\t}\n\n\t/**\n\t * 创建File对象\n\t *\n\t * @param uri 文件URI\n\t * @return File\n\t */\n\tpublic static File file(URI uri) {\n\t\tif (uri == null) {\n\t\t\tthrow new NullPointerException(\"File uri is null!\");\n\t\t}\n\t\treturn new File(uri);\n\t}\n\n\t/**\n\t * 创建File对象\n\t *\n\t * @param url 文件URL\n\t * @return File\n\t */\n\tpublic static File file(URL url) {\n\t\treturn new File(URLUtil.toURI(url));\n\t}\n\n\t/**\n\t * 获取临时文件路径（绝对路径）\n\t *\n\t * @return 临时文件路径","sourceCodeStart":362,"sourceCodeEnd":398,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/io/FileUtil.java#L362-L398","documentation":"FileUtil.file(URI) constructs a File from a URI. If the URI is null it throws NullPointerException(\"File uri is null!\") instead of letting new File(null) fail later. This is an explicit null precondition guard.","triggerScenarios":"Calling FileUtil.file((URI) null).","commonSituations":"Passing the result of ClassLoader.getResource().toURI() when the resource was not found (getResource returns null); a URI parsed from bad input that came back null; conditional code that left the URI unassigned.","solutions":["Null-check the URI before calling file().","When the URI comes from getResource(), treat null as 'resource not found' and handle that case explicitly.","Use Objects.requireNonNull(uri, \"uri\") for an early, clear failure."],"exampleFix":"// before: getResource may return null\nFile f = FileUtil.file(cls.getResource(\"/x.txt\").toURI()); // NPE\n\n// after\nURL url = cls.getResource(\"/x.txt\");\nif (url == null) {\n    throw new FileNotFoundException(\"/x.txt\");\n}\nFile f = FileUtil.file(url.toURI());","handlingStrategy":"validation","validationCode":"if (uri == null) {\n    throw new IllegalArgumentException(\"uri must not be null\");\n}\nFile f = FileUtil.file(uri);","typeGuard":"static boolean isResolvableUri(URI uri) {\n    return uri != null && uri.getScheme() != null;\n}","tryCatchPattern":"try {\n    return FileUtil.file(uri);\n} catch (NullPointerException e) {\n    // uri was null: handle missing-resource case\n}","preventionTips":["Null-check URIs sourced from getResource()/toURI() before calling file().","Treat a null getResource() result as 'not found' and handle explicitly.","Use Objects.requireNonNull(uri, \"uri\") for an early, clear failure."],"tags":["io","file","uri","null-safety"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}