{"record":{"id":"afc7e5d503faee5c","repo":"nostra13/Android-Universal-Image-Loader","slug":"imagesize-must-not-be-null","errorCode":null,"errorMessage":"imageSize must not be null","messagePattern":"imageSize must not be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"library/src/main/java/com/nostra13/universalimageloader/core/imageaware/NonViewAware.java","lineNumber":45,"sourceCode":" * used when user need just load and decode image and get it in {@linkplain\n * com.nostra13.universalimageloader.core.listener.ImageLoadingListener#onLoadingComplete(String, android.view.View,\n * android.graphics.Bitmap) callback}.\n *\n * @author Sergey Tarasevich (nostra13[at]gmail[dot]com)\n * @since 1.9.0\n */\npublic class NonViewAware implements ImageAware {\n\n\tprotected final String imageUri;\n\tprotected final ImageSize imageSize;\n\tprotected final ViewScaleType scaleType;\n\n\tpublic NonViewAware(ImageSize imageSize, ViewScaleType scaleType) {\n\t\tthis(null, imageSize, scaleType);\n\t}\n\n\tpublic NonViewAware(String imageUri, ImageSize imageSize, ViewScaleType scaleType) {\n\t\tif (imageSize == null) throw new IllegalArgumentException(\"imageSize must not be null\");\n\t\tif (scaleType == null) throw new IllegalArgumentException(\"scaleType must not be null\");\n\n\t\tthis.imageUri = imageUri;\n\t\tthis.imageSize = imageSize;\n\t\tthis.scaleType = scaleType;\n\t}\n\n\t@Override\n\tpublic int getWidth() {\n\t\treturn imageSize.getWidth();\n\t}\n\n\t@Override\n\tpublic int getHeight() {\n\t\treturn imageSize.getHeight();\n\t}\n\n\t@Override","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/nostra13/Android-Universal-Image-Loader/blob/ba33ec64d0daaa881d35852460e78c58d086bc18/library/src/main/java/com/nostra13/universalimageloader/core/imageaware/NonViewAware.java#L27-L63","documentation":"NonViewAware's constructor rejects a null ImageSize with IllegalArgumentException because every subsequent call (getWidth, getHeight, scaleType computations) dereferences it — the target size IS the whole point of a view-less ImageAware. It also rejects null ViewScaleType for the same reason. Failing fast here prevents an NPE deep inside the resize/decode pipeline.","triggerScenarios":"new NonViewAware(null, ViewScaleType.CROP) via the two-arg constructor, or new NonViewAware(uri, null, scaleType); building the ImageSize lazily after construction; passing a size obtained from a not-yet-laid-out view (e.g. getWidth()==0 is fine, but a null from a helper method is not).","commonSituations":"Prefetching images for widgets/notifications where no view exists; helper methods returning null on parse failure feeding the constructor; refactoring ViewAware code into NonViewAware and dropping the size argument.","solutions":["Compute a concrete ImageSize before constructing (e.g. new ImageSize(200, 200) or ImageSizeUtils.sizeToDecodeBounds)","Null-check any dynamically produced ImageSize and substitute a default rather than passing null","If you have no meaningful size, derive one from configuration (maxImageBounds) or your layout constraints","Also ensure scaleType is non-null — the same guard covers it"],"exampleFix":"// before\nImageSize size = parseSizeFromIntent(intent); // may return null\nNonViewAware aware = new NonViewAware(uri, size, ViewScaleType.CROP);\n\n// after\nImageSize size = parseSizeFromIntent(intent);\nif (size == null) size = new ImageSize(256, 256);\nNonViewAware aware = new NonViewAware(uri, size, ViewScaleType.CROP);","handlingStrategy":"validation","validationCode":"ImageSize size = resolveSize(); // may be null\nif (size == null) size = new ImageSize(256, 256);\nViewScaleType st = (scaleType != null) ? scaleType : ViewScaleType.CROP;\nNonViewAware aware = new NonViewAware(uri, size, st);","typeGuard":"private static boolean isNonViewAwareInputValid(ImageSize size, ViewScaleType st) {\n    return size != null && st != null;\n}","tryCatchPattern":null,"preventionTips":["Treat ImageSize and ViewScaleType as required fields of any prefetch request object; validate at request construction","Provide a default size constant instead of propagating null from parsers","Add @NonNull-style annotations or Objects.requireNonNull at the call site for clearer stack traces"],"tags":["universal-image-loader","imageaware","null-check","constructor"],"backgroundTag":null,"analyzedSha":"ba33ec64d0daaa881d35852460e78c58d086bc18","analyzedAt":"2026-08-14T15:41:15.893Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}