{"record":{"id":"69712d418b061759","repo":"stanfordnlp/CoreNLP","slug":"invalid-hex-color-for-blue-b","errorCode":null,"errorMessage":"invalid hex color for blue\"+b","messagePattern":"invalid hex color for blue\"\\+b","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ie/NERGUI.java","lineNumber":598,"sourceCode":"    editorPane.setEditable(true);\n\n  }\n\n  public static String colorToHTML(Color color) {\n    String r = Integer.toHexString(color.getRed());\n    if (r.length() == 0) { r = \"00\"; }\n    else if (r.length() == 1) { r = \"0\" + r; }\n    else if (r.length() > 2) { throw new IllegalArgumentException(\"invalid hex color for red\"+r); }\n\n    String g = Integer.toHexString(color.getGreen());\n    if (g.length() == 0) { g = \"00\"; }\n    else if (g.length() == 1) { g = \"0\" + g; }\n    else if (g.length() > 2) { throw new IllegalArgumentException(\"invalid hex color for green\"+g); }\n\n    String b = Integer.toHexString(color.getBlue());\n    if (b.length() == 0) { b = \"00\"; }\n    else if (b.length() == 1) { b = \"0\" + b; }\n    else if (b.length() > 2) { throw new IllegalArgumentException(\"invalid hex color for blue\"+b); }\n\n    return \"#\"+r+g+b;\n  }\n\n  static class ColorIcon implements Icon {\n    Color color;\n\n    public ColorIcon(Color c) {\n      color = c;\n    }\n\n    public void paintIcon(Component c, Graphics g, int x, int y) {\n      g.setColor(color);\n      g.fillRect(x, y, getIconWidth(), getIconHeight());\n    }\n\n    public int getIconWidth() {\n      return 10;","sourceCodeStart":580,"sourceCodeEnd":616,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/NERGUI.java#L580-L616","documentation":"NERGUI.colorToHTML throws IllegalArgumentException when the blue channel's hex string is longer than 2 characters. Same defensive invariant as the red/green checks: it should be unreachable for real java.awt.Color instances and signals a Color with a blue component outside 0-255.","triggerScenarios":"Calling colorToHTML with a Color whose getBlue() returns a value outside 0-255 (custom subclass or stub).","commonSituations":"Mocks in unit tests, custom Color subclasses with computed channel values.","solutions":["Use standard Color instances with valid 0-255 channels.","Clamp channel values before constructing the Color.","Use String.format(\"#%02x%02x%02x\", ...) instead of manual hex conversion."],"exampleFix":"// before\nthrow new IllegalArgumentException(\"invalid hex color for blue\"+b);\n// after\nreturn String.format(\"#%02x%02x%02x\", color.getRed(), color.getGreen(), color.getBlue());","handlingStrategy":"type-guard","validationCode":"static boolean isValidColor(Color c) {\n  return c.getBlue() >= 0 && c.getBlue() <= 255 && c.getRed() >= 0 && c.getRed() <= 255 && c.getGreen() >= 0 && c.getGreen() <= 255;\n}","typeGuard":"static boolean isStandardColor(Color c) { return c.getClass() == java.awt.Color.class; }","tryCatchPattern":"try {\n  String html = NERGUI.colorToHTML(color);\n} catch (IllegalArgumentException e) {\n  String html = String.format(\"#%02x%02x%02x\", color.getRed(), color.getGreen(), color.getBlue());\n}","preventionTips":["Use standard Color objects with 0-255 channels.","Clamp computed values before constructing Color.","Prefer String.format for HTML color output."],"tags":["swing","gui","illegal-argument","color","java"],"backgroundTag":"value-out-of-range","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}