{"record":{"id":"a1d82d134ce80799","repo":"nostra13/Android-Universal-Image-Loader","slug":"view-must-not-be-null","errorCode":null,"errorMessage":"view must not be null","messagePattern":"view must not be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/nostra13/universalimageloader/core/imageaware/ViewAware.java","lineNumber":70,"sourceCode":"\n\t/**\n\t * Constructor\n\t *\n\t * @param view                {@link android.view.View View} to work with\n\t * @param checkActualViewSize <b>true</b> - then {@link #getWidth()} and {@link #getHeight()} will check actual\n\t *                            size of View. It can cause known issues like\n\t *                            <a href=\"https://github.com/nostra13/Android-Universal-Image-Loader/issues/376\">this</a>.\n\t *                            But it helps to save memory because memory cache keeps bitmaps of actual (less in\n\t *                            general) size.\n\t *                            <p/>\n\t *                            <b>false</b> - then {@link #getWidth()} and {@link #getHeight()} will <b>NOT</b>\n\t *                            consider actual size of View, just layout parameters. <br /> If you set 'false'\n\t *                            it's recommended 'android:layout_width' and 'android:layout_height' (or\n\t *                            'android:maxWidth' and 'android:maxHeight') are set with concrete values. It helps to\n\t *                            save memory.\n\t */\n\tpublic ViewAware(View view, boolean checkActualViewSize) {\n\t\tif (view == null) throw new IllegalArgumentException(\"view must not be null\");\n\n\t\tthis.viewRef = new WeakReference<View>(view);\n\t\tthis.checkActualViewSize = checkActualViewSize;\n\t}\n\n\t/**\n\t * {@inheritDoc}\n\t * <p/>\n\t * Width is defined by target {@link android.view.View view} parameters, configuration\n\t * parameters or device display dimensions.<br />\n\t * Size computing algorithm (go by steps until get non-zero value):<br />\n\t * 1) Get the actual drawn <b>getWidth()</b> of the View<br />\n\t * 2) Get <b>layout_width</b>\n\t */\n\t@Override\n\tpublic int getWidth() {\n\t\tView view = viewRef.get();\n\t\tif (view != null) {","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/nostra13/Android-Universal-Image-Loader/blob/ba33ec64d0daaa881d35852460e78c58d086bc18/library/src/main/java/com/nostra13/universalimageloader/core/imageaware/ViewAware.java#L52-L88","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the findViewById result is non-null before wrapping (check id spelling and layout variant)","Only construct the aware after the view hierarchy is inflated (post-onCreateView / after setContentView)","Skip displayImage entirely when the target view is null instead of constructing the aware","Prefer passing the ImageView directly to displayImage(uri, imageView, ...) — it wraps internally at the right moment"],"exampleFix":"// before\nImageView avatar = (ImageView) convertView.findViewById(R.id.avatar); // null for this layout variant\nimageLoader.displayImage(uri, new ImageViewAware(avatar), options);\n\n// after\nImageView avatar = (ImageView) convertView.findViewById(R.id.avatar);\nif (avatar != null) {\n    imageLoader.displayImage(uri, avatar, options);\n}","handlingStrategy":"validation","validationCode":"ImageView target = (ImageView) itemView.findViewById(R.id.image);\nif (target != null) {\n    imageLoader.displayImage(uri, new ImageViewAware(target), options);\n} else {\n    Log.w(TAG, \"ImageView missing in layout; skipping load for \" + uri);\n}","typeGuard":"private static ImageViewAware toImageViewAwareOrNull(ImageView iv) {\n    return iv != null ? new ImageViewAware(iv) : null;\n}","tryCatchPattern":null,"preventionTips":["Null-check every findViewById before wrapping; verify R.id constants exist in every layout variant used by the adapter","Pass the ImageView straight to displayImage(uri, imageView, ...) and let UIL wrap it","Only build awares after the view hierarchy is inflated (after setContentView / onCreateView returns)","Treat a null view as 'skip this bind', never as 'wrap anyway'"],"tags":["universal-image-loader","imageaware","null-check","findviewbyid"],"backgroundTag":null,"analyzedSha":"ba33ec64d0daaa881d35852460e78c58d086bc18","analyzedAt":"2026-08-14T15:41:15.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}