nostra13/Android-Universal-Image-Loader · error · IllegalArgumentException

cacheDir argument must be not null

Error message

cacheDir argument must be not null

What it means

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).

Source

Thrown at library/src/main/java/com/nostra13/universalimageloader/cache/disc/impl/ext/LruDiskCache.java:85

	 */
	public LruDiskCache(File cacheDir, FileNameGenerator fileNameGenerator, long cacheMaxSize) throws IOException {
		this(cacheDir, null, fileNameGenerator, cacheMaxSize, 0);
	}

	/**
	 * @param cacheDir          Directory for file caching
	 * @param reserveCacheDir   null-ok; Reserve directory for file caching. It's used when the primary directory isn't available.
	 * @param fileNameGenerator {@linkplain com.nostra13.universalimageloader.cache.disc.naming.FileNameGenerator
	 *                          Name generator} for cached files. Generated names must match the regex
	 *                          <strong>[a-z0-9_-]{1,64}</strong>
	 * @param cacheMaxSize      Max cache size in bytes. <b>0</b> means cache size is unlimited.
	 * @param cacheMaxFileCount Max file count in cache. <b>0</b> means file count is unlimited.
	 * @throws IOException if cache can't be initialized (e.g. "No space left on device")
	 */
	public LruDiskCache(File cacheDir, File reserveCacheDir, FileNameGenerator fileNameGenerator, long cacheMaxSize,
			int cacheMaxFileCount) throws IOException {
		if (cacheDir == null) {
			throw new IllegalArgumentException("cacheDir" + ERROR_ARG_NULL);
		}
		if (cacheMaxSize < 0) {
			throw new IllegalArgumentException("cacheMaxSize" + ERROR_ARG_NEGATIVE);
		}
		if (cacheMaxFileCount < 0) {
			throw new IllegalArgumentException("cacheMaxFileCount" + ERROR_ARG_NEGATIVE);
		}
		if (fileNameGenerator == null) {
			throw new IllegalArgumentException("fileNameGenerator" + ERROR_ARG_NULL);
		}

		if (cacheMaxSize == 0) {
			cacheMaxSize = Long.MAX_VALUE;
		}
		if (cacheMaxFileCount == 0) {
			cacheMaxFileCount = Integer.MAX_VALUE;
		}

View on GitHub (pinned to ba33ec64d0)

Solutions

  1. Fall back to context.getCacheDir() when the external dir is null before building the configuration.
  2. Use DefaultConfigurationFactory.createDiskCache(context, ...) helpers, which handle directory resolution.
  3. In tests, supply a temporary directory instead of null.

Example fix

// before
File dir = context.getExternalCacheDir(); // null if storage unmounted
new LruDiskCache(dir, null, new HashCodeFileNameGenerator(), 50 * 1024 * 1024, 0);

// after
File dir = context.getExternalCacheDir();
if (dir == null) dir = context.getCacheDir();
new LruDiskCache(dir, null, new HashCodeFileNameGenerator(), 50 * 1024 * 1024, 0);
Defensive patterns

Strategy: validation

Validate before calling

File dir = context.getExternalCacheDir();
if (dir == null) dir = context.getCacheDir();
new LruDiskCache(dir, reserveDir, new HashCodeFileNameGenerator(), 50L * 1024 * 1024, 0);

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of nostra13/Android-Universal-Image-Loader@ba33ec64d0 (2026-08-14). Data as JSON: /api/errors/7e420d31870c14a9. Report an issue: GitHub.