{"record":{"id":"2e01770204ad3762","repo":"chinabugotech/hutool","slug":"error-2e0177","errorCode":null,"errorMessage":"图片流是空的","messagePattern":"图片流是空的","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/img/BackgroundRemoval.java","lineNumber":290,"sourceCode":"\tpublic static String getMainColor(File input) {\n\t\ttry {\n\t\t\treturn getMainColor(ImageIO.read(input));\n\t\t} catch (IOException e) {\n\t\t\te.printStackTrace();\n\t\t}\n\t\treturn \"\";\n\t}\n\n\t/**\n\t * 获取图片大概的主题色\n\t * 循环所有的像素点,取出出现次数最多的一个像素点的RGB值\n\t *\n\t * @param bufferedImage 图片流\n\t * @return 返回一个图片的大概的色值 一个16进制的颜色码\n\t */\n\tpublic static String getMainColor(BufferedImage bufferedImage) {\n\t\tif (bufferedImage == null) {\n\t\t\tthrow new IllegalArgumentException(\"图片流是空的\");\n\t\t}\n\n\t\t// 存储图片的所有RGB元素\n\t\tList<String> list = new ArrayList<>();\n\t\tfor (int y = bufferedImage.getMinY(); y < bufferedImage.getHeight(); y++) {\n\t\t\tfor (int x = bufferedImage.getMinX(); x < bufferedImage.getWidth(); x++) {\n\t\t\t\tint pixel = bufferedImage.getRGB(x, y);\n\t\t\t\tlist.add(((pixel & 0xff0000) >> 16) + \"-\" + ((pixel & 0xff00) >> 8) + \"-\" + (pixel & 0xff));\n\t\t\t}\n\t\t}\n\n\t\tfinal Map<String, Integer> map = new HashMap<>(list.size(), 1);\n\t\tfor (String string : list) {\n\t\t\tInteger integer = map.get(string);\n\t\t\tif (integer == null) {\n\t\t\t\tinteger = 1;\n\t\t\t} else {\n\t\t\t\tinteger++;","sourceCodeStart":272,"sourceCodeEnd":308,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/img/BackgroundRemoval.java#L272-L308","documentation":"BackgroundRemoval.getMainColor requires a non-null BufferedImage. Passing null throws IllegalArgumentException (message: '图片流是空的' = 'the image stream is empty'). It is a precondition check before iterating pixels.","triggerScenarios":"Calling ImgUtil.wrap/BackgroundRemoval.getMainColor with an image that failed to load (e.g. ImgUtil.read returned null, which it never does because it throws on null, but a user variable may be null); chaining operations where a prior step returned null.","commonSituations":"Loading an image from an untrusted path/URL that fails; decoding into BufferedImage that ends up null; calling getMainColor before validating the load result.","solutions":["Null-check the BufferedImage before calling getMainColor and handle the load failure at its source.","Use ImgUtil.read (File/URL/InputStream) which itself throws a clear error for unsupported types, ensuring a non-null image reaches getMainColor.","Validate the source file/URL exists and is a supported image format first."],"exampleFix":"// before\nString c = BackgroundRemoval.getMainColor(maybeNullImage);\n// after\nif (image == null) throw new IllegalStateException(\"image not loaded\");\nString c = BackgroundRemoval.getMainColor(image);","handlingStrategy":"validation","validationCode":"void requireImage(BufferedImage img){ if(img==null) throw new IllegalStateException(\"image not loaded\"); }","typeGuard":"boolean hasImage(BufferedImage img){ return img != null && img.getWidth() > 0 && img.getHeight() > 0; }","tryCatchPattern":"try { c = BackgroundRemoval.getMainColor(img); }\ncatch (IllegalArgumentException e){ if(e.getMessage().contains(\"图片流是空的\")) { /* reload image */ } else throw e; }","preventionTips":["Validate image load results before processing.","Wrap all image-read paths so null never propagates.","Fail fast at the source of the null."],"tags":["image","argument","null-check","background-removal"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}