{"record":{"id":"730db114db6eebea","repo":"didi/DoKit","slug":"width-must-be-positive-number-or-0","errorCode":null,"errorMessage":"Width must be positive number or 0.","messagePattern":"Width must be positive number or 0\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"Android/dokit/src/main/java/com/didichuxing/doraemonkit/picasso/Request.java","lineNumber":301,"sourceCode":"      return this;\n    }\n\n    /**\n     * Set the stable key to be used instead of the URI or resource ID when caching.\n     * Two requests with the same value are considered to be for the same resource.\n     */\n    public Builder stableKey(String stableKey) {\n      this.stableKey = stableKey;\n      return this;\n    }\n\n    /**\n     * Resize the image to the specified size in pixels.\n     * Use 0 as desired dimension to resize keeping aspect ratio.\n     */\n    public Builder resize(int targetWidth, int targetHeight) {\n      if (targetWidth < 0) {\n        throw new IllegalArgumentException(\"Width must be positive number or 0.\");\n      }\n      if (targetHeight < 0) {\n        throw new IllegalArgumentException(\"Height must be positive number or 0.\");\n      }\n      if (targetHeight == 0 && targetWidth == 0) {\n        throw new IllegalArgumentException(\"At least one dimension has to be positive number.\");\n      }\n      this.targetWidth = targetWidth;\n      this.targetHeight = targetHeight;\n      return this;\n    }\n\n    /** Clear the resize transformation, if any. This will also clear center crop/inside if set. */\n    public Builder clearResize() {\n      targetWidth = 0;\n      targetHeight = 0;\n      centerCrop = false;\n      centerInside = false;","sourceCodeStart":283,"sourceCodeEnd":319,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit/src/main/java/com/didichuxing/doraemonkit/picasso/Request.java#L283-L319","documentation":"Thrown by Request.Builder.resize(int, int) when targetWidth is negative. Picasso treats 0 as 'keep aspect ratio' for a dimension, so any value below 0 is rejected as meaningless for scaling. The check fires immediately at the resize() call, before build().","triggerScenarios":"Calling resize(-1, 100) or any resize() where the first argument is < 0 on a RequestCreator/Request.Builder.","commonSituations":"Passing view layout constants such as ViewGroup.LayoutParams.MATCH_PARENT (-1) or WRAP_CONTENT (-2) directly as pixel sizes; computing width from an unmeasured view (getWidth() returns 0 or a negative derived value); forwarding a dimension from code that uses -1 as 'unspecified'.","solutions":["Clamp negative values to 0 before calling resize (0 legally means 'keep aspect ratio'): resize(Math.max(0, w), Math.max(0, h)).","Never pass MATCH_PARENT/WRAP_CONTENT; resolve the view's measured size first (view.getWidth() after layout) or use getMeasuredWidth().","If the size is unknown at request time, defer the request (Picasso's fit()) or pass only one dimension as 0."],"exampleFix":"// before\nint w = imageView.getLayoutParams().width; // may be -1 (MATCH_PARENT)\npicasso.load(url).resize(w, 400).into(imageView);\n\n// after\nint w = Math.max(0, imageView.getWidth()); // measured pixels, 0 = keep ratio\npicasso.load(url).resize(w, 400).into(imageView);","handlingStrategy":"validation","validationCode":"int safeWidth = Math.max(0, targetWidth); // 0 = keep aspect ratio\nbuilder.resize(safeWidth, targetHeight);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never forward LayoutParams constants (MATCH_PARENT=-1, WRAP_CONTENT=-2) as pixel dimensions.","Clamp any externally computed dimension to >= 0 before resize().","Remember Picasso's convention: 0 means 'derive this dimension automatically'."],"tags":["android","picasso","image-loading","validation","builder"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}