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

view must not be null

Error message

view must not be null

What it means

ViewAware's constructor requires a non-null View and stores it in a WeakReference; a null view would make every later operation (width/height queries, drawable setting) impossible, so it fails fast with IllegalArgumentException. This guard runs before any image loading starts, at the moment you wrap a view. Subclasses like ImageViewAware inherit the same check.

Source

Thrown at library/src/main/java/com/nostra13/universalimageloader/core/imageaware/ViewAware.java:70

	/**
	 * Constructor
	 *
	 * @param view                {@link android.view.View View} to work with
	 * @param checkActualViewSize <b>true</b> - then {@link #getWidth()} and {@link #getHeight()} will check actual
	 *                            size of View. It can cause known issues like
	 *                            <a href="https://github.com/nostra13/Android-Universal-Image-Loader/issues/376">this</a>.
	 *                            But it helps to save memory because memory cache keeps bitmaps of actual (less in
	 *                            general) size.
	 *                            <p/>
	 *                            <b>false</b> - then {@link #getWidth()} and {@link #getHeight()} will <b>NOT</b>
	 *                            consider actual size of View, just layout parameters. <br /> If you set 'false'
	 *                            it's recommended 'android:layout_width' and 'android:layout_height' (or
	 *                            'android:maxWidth' and 'android:maxHeight') are set with concrete values. It helps to
	 *                            save memory.
	 */
	public ViewAware(View view, boolean checkActualViewSize) {
		if (view == null) throw new IllegalArgumentException("view must not be null");

		this.viewRef = new WeakReference<View>(view);
		this.checkActualViewSize = checkActualViewSize;
	}

	/**
	 * {@inheritDoc}
	 * <p/>
	 * Width is defined by target {@link android.view.View view} parameters, configuration
	 * parameters or device display dimensions.<br />
	 * Size computing algorithm (go by steps until get non-zero value):<br />
	 * 1) Get the actual drawn <b>getWidth()</b> of the View<br />
	 * 2) Get <b>layout_width</b>
	 */
	@Override
	public int getWidth() {
		View view = viewRef.get();
		if (view != null) {

View on GitHub (pinned to ba33ec64d0)

Solutions

  1. Verify the findViewById result is non-null before wrapping (check id spelling and layout variant)
  2. Only construct the aware after the view hierarchy is inflated (post-onCreateView / after setContentView)
  3. Skip displayImage entirely when the target view is null instead of constructing the aware
  4. Prefer passing the ImageView directly to displayImage(uri, imageView, ...) — it wraps internally at the right moment

Example fix

// before
ImageView avatar = (ImageView) convertView.findViewById(R.id.avatar); // null for this layout variant
imageLoader.displayImage(uri, new ImageViewAware(avatar), options);

// after
ImageView avatar = (ImageView) convertView.findViewById(R.id.avatar);
if (avatar != null) {
    imageLoader.displayImage(uri, avatar, options);
}
Defensive patterns

Strategy: validation

Validate before calling

ImageView target = (ImageView) itemView.findViewById(R.id.image);
if (target != null) {
    imageLoader.displayImage(uri, new ImageViewAware(target), options);
} else {
    Log.w(TAG, "ImageView missing in layout; skipping load for " + uri);
}

Type guard

private static ImageViewAware toImageViewAwareOrNull(ImageView iv) {
    return iv != null ? new ImageViewAware(iv) : null;
}

Prevention

When it happens

Trigger: new ImageViewAware(null); wrapping a view in ImageAware after findViewById returned null (wrong id, view not yet inflated, or findViewById on the wrong fragment view); constructing awares in a loop over nullable view holders; passing a view from a destroyed fragment/dialog.

Common situations: ViewHolder convertView reuse patterns where a row layout lacks the expected ImageView; findViewById(R.id.x) typos returning null; wrapping views inside Fragment.onCreateView before inflation completed; RecyclerView adapters binding before layout.

Related errors


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