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

fileNameGenerator argument must be not null

Error message

fileNameGenerator argument must be not null

What it means

LruDiskCache's constructor throws IllegalArgumentException when fileNameGenerator is null. The generator's output becomes the DiskLruCache key and must match [a-z0-9_-]{1,64}; the cache cannot name files without it, so construction fails fast. LruDiskCache.initDiskCache passes generator output straight into DiskLruCache, which independently enforces the key regex.

Source

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

	 *                          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;
		}

		this.reserveCacheDir = reserveCacheDir;
		this.fileNameGenerator = fileNameGenerator;
		initCache(cacheDir, reserveCacheDir, cacheMaxSize, cacheMaxFileCount);
	}

	private void initCache(File cacheDir, File reserveCacheDir, long cacheMaxSize, int cacheMaxFileCount)
			throws IOException {
		try {
			cache = DiskLruCache.open(cacheDir, 1, 1, cacheMaxSize, cacheMaxFileCount);

View on GitHub (pinned to ba33ec64d0)

Solutions

  1. Pass a generator: new HashCodeFileNameGenerator() or new Md5FileNameGenerator().
  2. Or use ImageLoaderConfiguration.builder(context).diskCache(new LruDiskCache(dir, reserve, new HashCodeFileNameGenerator(), size, 0)) with an explicit, non-null generator.
  3. Add a DI guard/requireNonNull on the generator binding so failures surface at wiring time.

Example fix

// before
new LruDiskCache(dir, null, null, 50 * 1024 * 1024, 0);

// after
new LruDiskCache(dir, null, new HashCodeFileNameGenerator(), 50 * 1024 * 1024, 0);
Defensive patterns

Strategy: validation

Validate before calling

FileNameGenerator gen = Objects.requireNonNull(generator, "fileNameGenerator");
new LruDiskCache(dir, reserve, gen, 50L * 1024 * 1024, 0);

Prevention

When it happens

Trigger: Calling new LruDiskCache(cacheDir, reserve, null, maxSize, maxFileCount). Higher-level UIL flows supply DefaultConfigurationFactory.createFileNameGenerator() automatically, so this occurs in custom construction code or broken DI wiring.

Common situations: Hand-rolled LruDiskCache setup that omits the generator argument handling; DI containers missing a FileNameGenerator binding; refactors that null out the generator 'temporarily'.

Related errors


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