{"record":{"id":"39c248543721761b","repo":"lingochamp/FileDownloader","slug":"can-t-generate-real-path-the-file-name-is-null","errorCode":null,"errorMessage":"can't generate real path, the file name is null","messagePattern":"can't generate real path, the file name is null","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/liulishuo/filedownloader/util/FileDownloadUtils.java","lineNumber":176,"sourceCode":"        } else {\n            return FileDownloadHelper.getAppContext().getCacheDir().getAbsolutePath();\n        }\n    }\n\n    public static String getDefaultSaveFilePath(final String url) {\n        return generateFilePath(getDefaultSaveRootPath(), generateFileName(url));\n    }\n\n    public static String generateFileName(final String url) {\n        return md5(url);\n    }\n\n    /**\n     * @see #getTargetFilePath(String, boolean, String)\n     */\n    public static String generateFilePath(String directory, String filename) {\n        if (filename == null) {\n            throw new IllegalStateException(\"can't generate real path, the file name is null\");\n        }\n\n        if (directory == null) {\n            throw new IllegalStateException(\"can't generate real path, the directory is null\");\n        }\n\n        return formatString(\"%s%s%s\", directory, File.separator, filename);\n    }\n\n    /**\n     * The path is used as the default directory in the case of the task without set path.\n     *\n     * @param path default root path for save download file.\n     * @see com.liulishuo.filedownloader.BaseDownloadTask#setPath(String, boolean)\n     */\n    public static void setDefaultSaveRootPath(final String path) {\n        defaultSaveRootPath = path;\n    }","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/lingochamp/FileDownloader/blob/6237a8cac174bcc916e4342b14ab1ab72a5768d4/library/src/main/java/com/liulishuo/filedownloader/util/FileDownloadUtils.java#L158-L194","documentation":"generateFilePath builds the absolute save path as <directory>/<filename>. A null filename cannot produce a valid path, so the library fails fast with an IllegalStateException instead of returning a broken path. It is thrown before the directory check, so the filename is the first thing validated.","triggerScenarios":"Calling FileDownloadUtils.generateFilePath(directory, null), directly or via getDefaultSaveFilePath when the task/connection yields no filename.","commonSituations":"Passing a task whose filename was never set (no FileDownloadTask.path(...) and server response lacks Content-Disposition so filename inference fails); calling the util directly with an uninitialized variable.","solutions":["Provide a filename: call task.setPath(...) or pass a non-null filename to generateFilePath.","If the filename comes from the server, ensure the response includes Content-Disposition or that generateFileName(url) can derive one from the URL.","Guard the call site: if (filename != null) generateFilePath(directory, filename) else fall back to generateFileName(url)."],"exampleFix":"// before\nString path = FileDownloadUtils.generateFilePath(dir, task.getFilename()); // getFilename() == null\n\n// after\nString filename = task.getFilename() != null ? task.getFilename() : FileDownloadUtils.generateFileName(url);\nString path = FileDownloadUtils.generateFilePath(dir, filename);","handlingStrategy":"type-guard","validationCode":"if (filename == null) {\n    filename = FileDownloadUtils.generateFileName(url);\n}\nString path = FileDownloadUtils.generateFilePath(directory, filename);","typeGuard":"boolean hasFilename(String f) { return f != null && !f.trim().isEmpty(); }","tryCatchPattern":"try {\n    return FileDownloadUtils.generateFilePath(directory, filename);\n} catch (IllegalStateException e) {\n    return FileDownloadUtils.generateFilePath(directory, FileDownloadUtils.generateFileName(url));\n}","preventionTips":["Always set task.setPath(...) or ensure the server supplies Content-Disposition.","Null-check any filename sourced from headers/URL before path generation.","Prefer explicit paths over server-derived names for predictable behavior."],"tags":["android","null-argument","path-generation","filedownloader"],"backgroundTag":"null-argument","analyzedSha":"6237a8cac174bcc916e4342b14ab1ab72a5768d4","analyzedAt":"2026-09-08T23:50:48.168Z","contentChangedAt":"2026-09-08T23:50:48.168Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}