{"record":{"id":"7e420d31870c14a9","repo":"nostra13/Android-Universal-Image-Loader","slug":"cachedir-argument-must-be-not-null-7e420d","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/ext/LruDiskCache.java","lineNumber":85,"sourceCode":"\t */\n\tpublic LruDiskCache(File cacheDir, FileNameGenerator fileNameGenerator, long cacheMaxSize) throws IOException {\n\t\tthis(cacheDir, null, fileNameGenerator, cacheMaxSize, 0);\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. Generated names must match the regex\n\t *                          <strong>[a-z0-9_-]{1,64}</strong>\n\t * @param cacheMaxSize      Max cache size in bytes. <b>0</b> means cache size is unlimited.\n\t * @param cacheMaxFileCount Max file count in cache. <b>0</b> means file count is unlimited.\n\t * @throws IOException if cache can't be initialized (e.g. \"No space left on device\")\n\t */\n\tpublic LruDiskCache(File cacheDir, File reserveCacheDir, FileNameGenerator fileNameGenerator, long cacheMaxSize,\n\t\t\tint cacheMaxFileCount) throws IOException {\n\t\tif (cacheDir == null) {\n\t\t\tthrow new IllegalArgumentException(\"cacheDir\" + ERROR_ARG_NULL);\n\t\t}\n\t\tif (cacheMaxSize < 0) {\n\t\t\tthrow new IllegalArgumentException(\"cacheMaxSize\" + ERROR_ARG_NEGATIVE);\n\t\t}\n\t\tif (cacheMaxFileCount < 0) {\n\t\t\tthrow new IllegalArgumentException(\"cacheMaxFileCount\" + ERROR_ARG_NEGATIVE);\n\t\t}\n\t\tif (fileNameGenerator == null) {\n\t\t\tthrow new IllegalArgumentException(\"fileNameGenerator\" + ERROR_ARG_NULL);\n\t\t}\n\n\t\tif (cacheMaxSize == 0) {\n\t\t\tcacheMaxSize = Long.MAX_VALUE;\n\t\t}\n\t\tif (cacheMaxFileCount == 0) {\n\t\t\tcacheMaxFileCount = Integer.MAX_VALUE;\n\t\t}\n","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/nostra13/Android-Universal-Image-Loader/blob/ba33ec64d0daaa881d35852460e78c58d086bc18/library/src/main/java/com/nostra13/universalimageloader/cache/disc/impl/ext/LruDiskCache.java#L67-L103","documentation":"LruDiskCache's constructor throws IllegalArgumentException when cacheDir is null. LruDiskCache wraps DiskLruCache and must open a journal in that directory during initCache, so the primary directory is mandatory; only reserveCacheDir may be null (it is a fallback when the primary dir is unavailable).","triggerScenarios":"Constructing LruDiskCache directly with a null first argument, or via ImageLoaderConfiguration with a disk cache directory that resolved to null — typically context.getExternalCacheDir() returning null because external storage is unmounted.","commonSituations":"Configuring UIL with getExternalCacheDir() on devices where external storage is missing/being swapped; host-JVM unit tests where no real cache dir exists; initialization order bugs where the cache is built before directories are known.","solutions":["Fall back to context.getCacheDir() when the external dir is null before building the configuration.","Use DefaultConfigurationFactory.createDiskCache(context, ...) helpers, which handle directory resolution.","In tests, supply a temporary directory instead of null."],"exampleFix":"// before\nFile dir = context.getExternalCacheDir(); // null if storage unmounted\nnew LruDiskCache(dir, null, new HashCodeFileNameGenerator(), 50 * 1024 * 1024, 0);\n\n// after\nFile dir = context.getExternalCacheDir();\nif (dir == null) dir = context.getCacheDir();\nnew LruDiskCache(dir, null, new HashCodeFileNameGenerator(), 50 * 1024 * 1024, 0);","handlingStrategy":"validation","validationCode":"File dir = context.getExternalCacheDir();\nif (dir == null) dir = context.getCacheDir();\nnew LruDiskCache(dir, reserveDir, new HashCodeFileNameGenerator(), 50L * 1024 * 1024, 0);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always null-check getExternalCacheDir() — unmounted external storage makes it null.","Build disk caches only after the Application context is ready, never from static initializers with unresolved paths.","In tests, supply a temp directory rather than null."],"tags":["disk-cache","lru","validation","constructor","null-check","android"],"backgroundTag":null,"analyzedSha":"ba33ec64d0daaa881d35852460e78c58d086bc18","analyzedAt":"2026-08-14T15:41:15.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}