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

cacheDir argument must be not null

Error message

cacheDir argument must be not null

What it means

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.

Source

Thrown at library/src/main/java/com/nostra13/universalimageloader/cache/disc/impl/BaseDiskCache.java:80

	}

	/**
	 * @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.
	 */
	public BaseDiskCache(File cacheDir, File reserveCacheDir) {
		this(cacheDir, reserveCacheDir, DefaultConfigurationFactory.createFileNameGenerator());
	}

	/**
	 * @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
	 */
	public BaseDiskCache(File cacheDir, File reserveCacheDir, FileNameGenerator fileNameGenerator) {
		if (cacheDir == null) {
			throw new IllegalArgumentException("cacheDir" + ERROR_ARG_NULL);
		}
		if (fileNameGenerator == null) {
			throw new IllegalArgumentException("fileNameGenerator" + ERROR_ARG_NULL);
		}

		this.cacheDir = cacheDir;
		this.reserveCacheDir = reserveCacheDir;
		this.fileNameGenerator = fileNameGenerator;
	}

	@Override
	public File getDirectory() {
		return cacheDir;
	}

	@Override
	public File get(String imageUri) {
		return getFile(imageUri);

View on GitHub (pinned to ba33ec64d0)

Solutions

  1. Ensure the File passed as cacheDir is non-null before constructing the cache, e.g. fall back to context.getCacheDir() when getExternalCacheDir() returns null.
  2. If the directory comes from configuration, validate it early in your ImageLoaderConfiguration builder and fail with your own clearer message.
  3. In tests, point cacheDir at a temporary folder (e.g. JUnit TemporaryFolder) instead of a nullable mock.

Example fix

// before
File dir = context.getExternalCacheDir(); // null when external storage unavailable
DiskCache cache = new UnlimitedDiskCache(dir);

// after
File dir = context.getExternalCacheDir();
if (dir == null) dir = context.getCacheDir();
DiskCache cache = new UnlimitedDiskCache(dir);
Defensive patterns

Strategy: validation

Validate before calling

File cacheDir = (external ? context.getExternalCacheDir() : context.getCacheDir());
if (cacheDir == null) cacheDir = context.getCacheDir(); // internal dir is never null
// now safe: new UnlimitedDiskCache(cacheDir)

Prevention

When it happens

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

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

Related errors


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