{"record":{"id":"3afb5527dcda209e","repo":"nostra13/Android-Universal-Image-Loader","slug":"cachedir-argument-must-be-not-null","errorCode":null,"errorMessage":"cacheDir argument must be not null","messagePattern":"cacheDir argument must be not null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/nostra13/universalimageloader/cache/disc/impl/BaseDiskCache.java","lineNumber":80,"sourceCode":"\t}\n\n\t/**\n\t * @param cacheDir        Directory for file caching\n\t * @param reserveCacheDir null-ok; Reserve directory for file caching. It's used when the primary directory isn't available.\n\t */\n\tpublic BaseDiskCache(File cacheDir, File reserveCacheDir) {\n\t\tthis(cacheDir, reserveCacheDir, DefaultConfigurationFactory.createFileNameGenerator());\n\t}\n\n\t/**\n\t * @param cacheDir          Directory for file caching\n\t * @param reserveCacheDir   null-ok; Reserve directory for file caching. It's used when the primary directory isn't available.\n\t * @param fileNameGenerator {@linkplain com.nostra13.universalimageloader.cache.disc.naming.FileNameGenerator\n\t *                          Name generator} for cached files\n\t */\n\tpublic BaseDiskCache(File cacheDir, File reserveCacheDir, FileNameGenerator fileNameGenerator) {\n\t\tif (cacheDir == null) {\n\t\t\tthrow new IllegalArgumentException(\"cacheDir\" + ERROR_ARG_NULL);\n\t\t}\n\t\tif (fileNameGenerator == null) {\n\t\t\tthrow new IllegalArgumentException(\"fileNameGenerator\" + ERROR_ARG_NULL);\n\t\t}\n\n\t\tthis.cacheDir = cacheDir;\n\t\tthis.reserveCacheDir = reserveCacheDir;\n\t\tthis.fileNameGenerator = fileNameGenerator;\n\t}\n\n\t@Override\n\tpublic File getDirectory() {\n\t\treturn cacheDir;\n\t}\n\n\t@Override\n\tpublic File get(String imageUri) {\n\t\treturn getFile(imageUri);","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/nostra13/Android-Universal-Image-Loader/blob/ba33ec64d0daaa881d35852460e78c58d086bc18/library/src/main/java/com/nostra13/universalimageloader/cache/disc/impl/BaseDiskCache.java#L62-L98","documentation":"BaseDiskCache's constructor throws IllegalArgumentException when the primary cache directory argument is null. Unlike reserveCacheDir (documented null-ok), cacheDir is mandatory because every save/get operation resolves files under it. This is a fail-fast guard on cache construction, before any disk I/O happens.","triggerScenarios":"Calling new BaseDiskCache(null, reserve), new BaseDiskCache(null, reserve, nameGenerator), or subclass constructors (e.g. UnlimitedDiskCache) with a null cacheDir. Typically happens when context.getCacheDir() returns null or a File field was never initialized before building the disk cache.","commonSituations":"Unit tests on a host JVM where getCacheDir() is null/stubbed; passing getExternalCacheDir() on devices where external storage is not mounted (returns null); copy-pasted configuration code that builds the cache before the directory is computed.","solutions":["Ensure the File passed as cacheDir is non-null before constructing the cache, e.g. fall back to context.getCacheDir() when getExternalCacheDir() returns null.","If the directory comes from configuration, validate it early in your ImageLoaderConfiguration builder and fail with your own clearer message.","In tests, point cacheDir at a temporary folder (e.g. JUnit TemporaryFolder) instead of a nullable mock."],"exampleFix":"// before\nFile dir = context.getExternalCacheDir(); // null when external storage unavailable\nDiskCache cache = new UnlimitedDiskCache(dir);\n\n// after\nFile dir = context.getExternalCacheDir();\nif (dir == null) dir = context.getCacheDir();\nDiskCache cache = new UnlimitedDiskCache(dir);","handlingStrategy":"validation","validationCode":"File cacheDir = (external ? context.getExternalCacheDir() : context.getCacheDir());\nif (cacheDir == null) cacheDir = context.getCacheDir(); // internal dir is never null\n// now safe: new UnlimitedDiskCache(cacheDir)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never pass getExternalCacheDir() directly — it returns null when external storage is unmounted; chain a fallback to context.getCacheDir().","Centralize cache-directory resolution in one helper so every construction path shares the null-safe logic.","In unit tests, use a temporary folder (JUnit @Rule TemporaryFolder) instead of mocking File."],"tags":["disk-cache","validation","constructor","null-check","android"],"backgroundTag":null,"analyzedSha":"ba33ec64d0daaa881d35852460e78c58d086bc18","analyzedAt":"2026-08-14T15:41:15.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}