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

key == null

Error message

key == null

What it means

LruMemoryCache.get(String) rejects a null key with NullPointerException because the underlying LinkedHashMap access-order bookkeeping requires a real key, and silently returning null would mask caller bugs. Keys in UIL are the (non-null) URI strings generated internally by MemoryCacheUtil, so a null key reaching this method means the caller bypassed the normal pipeline.

Source

Thrown at library/src/main/java/com/nostra13/universalimageloader/cache/memory/impl/LruMemoryCache.java:46

	private int size;

	/** @param maxSize Maximum sum of the sizes of the Bitmaps in this cache */
	public LruMemoryCache(int maxSize) {
		if (maxSize <= 0) {
			throw new IllegalArgumentException("maxSize <= 0");
		}
		this.maxSize = maxSize;
		this.map = new LinkedHashMap<String, Bitmap>(0, 0.75f, true);
	}

	/**
	 * Returns the Bitmap for {@code key} if it exists in the cache. If a Bitmap was returned, it is moved to the head
	 * of the queue. This returns null if a Bitmap is not cached.
	 */
	@Override
	public final Bitmap get(String key) {
		if (key == null) {
			throw new NullPointerException("key == null");
		}

		synchronized (this) {
			return map.get(key);
		}
	}

	/** Caches {@code Bitmap} for {@code key}. The Bitmap is moved to the head of the queue. */
	@Override
	public final boolean put(String key, Bitmap value) {
		if (key == null || value == null) {
			throw new NullPointerException("key == null || value == null");
		}

		synchronized (this) {
			size += sizeOf(key, value);
			Bitmap previous = map.put(key, value);
			if (previous != null) {

View on GitHub (pinned to ba33ec64d0)

Solutions

  1. Null-check the key before calling get: if (uri != null) cache.get(uri)
  2. Route image access through ImageLoader.displayImage/loadImage, which null-checks URIs upstream
  3. Fix the source producing null URIs (missing intent extras, optional JSON fields)

Example fix

// before
Bitmap b = ImageLoader.getInstance().getMemoryCache().get(uri); // uri may be null

// after
Bitmap b = uri != null ? ImageLoader.getInstance().getMemoryCache().get(uri) : null;
if (b == null) ImageLoader.getInstance().displayImage(uri, imageView);
Defensive patterns

Strategy: type-guard

Validate before calling

if (uri != null) {
    Bitmap b = ImageLoader.getInstance().getMemoryCache().get(uri);
}

Type guard

static boolean isCacheableKey(String key) {
    return key != null && key.length() > 0;
}

Prevention

When it happens

Trigger: Directly calling memoryCache.get(null) on the cache obtained from ImageLoader.getMemoryCache(); passing a null String uri into custom code that forwards it as a cache key; a custom MemoryCacheAware wrapper that forwards null after a failed key generation.

Common situations: Custom caches or instrumentation code calling getMemoryCache().get(uri) with a uri variable that is null because the intent extra / model field was absent; tests poking the cache directly.

Related errors


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