nostra13/Android-Universal-Image-Loader · error · IllegalArgumentException
fileNameGenerator argument must be not null
Error message
fileNameGenerator argument must be not null
What it means
BaseDiskCache's constructor throws IllegalArgumentException when fileNameGenerator is null. The generator converts URLs into cache file names, so every read/write on the disk cache depends on it; it cannot be defaulted safely at this layer. The check is fail-fast at construction time.
Source
Thrown at library/src/main/java/com/nostra13/universalimageloader/cache/disc/impl/BaseDiskCache.java:83
* @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);
}
@OverrideView on GitHub (pinned to ba33ec64d0)
Solutions
- Pass a real generator: new HashCodeFileNameGenerator() (or HashCodeFileNameGenerator via DefaultConfigurationFactory.createFileNameGenerator()).
- If you don't need custom naming, use the shorter constructors BaseDiskCache(cacheDir) or BaseDiskCache(cacheDir, reserveCacheDir) which apply the default generator.
- Check custom subclasses: ensure no constructor path forwards null.
Example fix
// before new BaseDiskCache(cacheDir, reserveCacheDir, null); // after new BaseDiskCache(cacheDir, reserveCacheDir, new HashCodeFileNameGenerator());
Defensive patterns
Strategy: validation
Validate before calling
FileNameGenerator gen = (generator != null) ? generator : new HashCodeFileNameGenerator(); new BaseDiskCache(cacheDir, reserveCacheDir, gen);
Prevention
- Prefer the short constructors BaseDiskCache(cacheDir[, reserve]) which install a default generator.
- Add Objects.requireNonNull(fileNameGenerator, 'fileNameGenerator') at your own construction wrapper so failures point at your call site.
- Keep DI bindings for FileNameGenerator explicit and required, not optional.
When it happens
Trigger: Calling new BaseDiskCache(cacheDir, reserve, null) directly, or a subclass constructor that forwards a null generator. The one- and two-arg convenience constructors avoid it by defaulting to DefaultConfigurationFactory.createFileNameGenerator().
Common situations: Custom DiskCache implementations that thread a generator through several layers and forget to initialize it; refactors that removed the generator parameter but left a null passthrough; DI setups where the FileNameGenerator binding is missing.
Related errors
- fileNameGenerator argument must be not null
- cacheDir argument must be not null
- cacheDir argument must be not null
- maxSize <= 0
- keys must match regex [a-z0-9_-]{1,64}: "{key}"
AI-assisted analysis of nostra13/Android-Universal-Image-Loader@ba33ec64d0 (2026-08-14).
Data as JSON: /api/errors/c259f6d5d9bba85c.
Report an issue: GitHub.