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

bitmapConfig can't be null

Error message

bitmapConfig can't be null

What it means

DisplayImageOptions.Builder.bitmapConfig(Bitmap.Config) rejects null because the value is written directly into the BitmapFactory.Options instance (decodingOptions.inPreferredConfig) that is reused for every decode; a null there would defeat the whole builder chain and crash later at decode time with a less clear error. Only real configs (ALPHA_8, RGB_565, ARGB_8888) are accepted.

Source

Thrown at library/src/main/java/com/nostra13/universalimageloader/core/DisplayImageOptions.java:377

		/** Sets whether loaded image will be cached on disk */
		public Builder cacheOnDisk(boolean cacheOnDisk) {
			this.cacheOnDisk = cacheOnDisk;
			return this;
		}

		/**
		 * Sets {@linkplain ImageScaleType scale type} for decoding image. This parameter is used while define scale
		 * size for decoding image to Bitmap. Default value - {@link ImageScaleType#IN_SAMPLE_POWER_OF_2}
		 */
		public Builder imageScaleType(ImageScaleType imageScaleType) {
			this.imageScaleType = imageScaleType;
			return this;
		}

		/** Sets {@link Bitmap.Config bitmap config} for image decoding. Default value - {@link Bitmap.Config#ARGB_8888} */
		public Builder bitmapConfig(Bitmap.Config bitmapConfig) {
			if (bitmapConfig == null) throw new IllegalArgumentException("bitmapConfig can't be null");
			decodingOptions.inPreferredConfig = bitmapConfig;
			return this;
		}

		/**
		 * Sets options for image decoding.<br />
		 * <b>NOTE:</b> {@link Options#inSampleSize} of incoming options will <b>NOT</b> be considered. Library
		 * calculate the most appropriate sample size itself according yo {@link #imageScaleType(ImageScaleType)}
		 * options.<br />
		 * <b>NOTE:</b> This option overlaps {@link #bitmapConfig(android.graphics.Bitmap.Config) bitmapConfig()}
		 * option.
		 */
		public Builder decodingOptions(Options decodingOptions) {
			if (decodingOptions == null) throw new IllegalArgumentException("decodingOptions can't be null");
			this.decodingOptions = decodingOptions;
			return this;
		}

View on GitHub (pinned to ba33ec64d0)

Solutions

  1. Default the variable: config = (config != null) ? config : Bitmap.Config.ARGB_8888
  2. Model quality as an explicit choice: ARGB_8888 for quality, RGB_565 for memory, never null
  3. Delete the branch that can produce null (e.g. replace ternary with a two-branch if/else)

Example fix

// before
Bitmap.Config cfg = prefs.getBoolean("hq") ? Bitmap.Config.ARGB_8888 : null;
options.bitmapConfig(cfg); // throws bitmapConfig can't be null

// after
Bitmap.Config cfg = prefs.getBoolean("hq") ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565;
options.bitmapConfig(cfg);
Defensive patterns

Strategy: validation

Validate before calling

Bitmap.Config cfg = (chosenConfig != null) ? chosenConfig : Bitmap.Config.ARGB_8888;
options.bitmapConfig(cfg);

Type guard

static boolean isValidBitmapConfig(Bitmap.Config c) {
    return c != null; // ALPHA_8, RGB_565, ARGB_8888 are the meaningful values
}

Prevention

When it happens

Trigger: Calling .bitmapConfig(null) in a DisplayImageOptions.Builder chain, usually when reading the config from a variable/JSON setting that was never populated; conditional code like config.useHighQuality ? ARGB_8888 : null.

Common situations: Quality toggles implemented as 'high quality or null'; deserializing a user preference where an unknown enum string maps to null; refactors that made the field @Nullable but kept the direct pass-through.

Related errors


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