{"record":{"id":"0b3787cf82538013","repo":"stanfordnlp/CoreNLP","slug":"invalid-hex-color-for-red-r","errorCode":null,"errorMessage":"invalid hex color for red\"+r","messagePattern":"invalid hex color for red\"\\+r","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ie/NERGUI.java","lineNumber":588,"sourceCode":"    editorPane.setDocument(doc);\n    try {\n      doc.insertString(0, initText, defaultAttrSet);\n    } catch (Exception ex) {\n      throw new RuntimeException(ex);\n    }\n\n    JScrollPane scrollPane = new JScrollPane(editorPane);\n    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);\n\n    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) {","sourceCodeStart":570,"sourceCodeEnd":606,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/NERGUI.java#L570-L606","documentation":"NERGUI.colorToHTML converts a java.awt.Color to a '#rrggbb' string. If Integer.toHexString of the red component yields a string longer than 2 characters it throws IllegalArgumentException — logically impossible for getRed() (0-255), so this only triggers if a Color subclass or corrupt value yields an unexpected value. It is an internal sanity check on the red channel.","triggerScenarios":"Calling NERGUI.colorToHTML(color) with a Color whose red component's hex representation is longer than 2 chars — practically only via a buggy Color subclass overriding getRed() with a value outside 0-255.","commonSituations":"Passing a mock/stub Color with out-of-range channel values in tests, or a Color subclass returning values outside 0-255.","solutions":["Ensure the Color argument is a standard java.awt.Color constructed with channel values in 0-255.","Clamp or validate channel values before constructing the Color.","Replace with String.format(\"#%02x%02x%02x\", r, g, b) which never throws for such ranges."],"exampleFix":"// before\nthrow new IllegalArgumentException(\"invalid hex color for red\"+r);\n// after\nr = r.substring(r.length()-2); // or use String.format(\"#%02x%02x%02x\", color.getRed(), color.getGreen(), color.getBlue());","handlingStrategy":"type-guard","validationCode":"static boolean isValidColor(Color c) {\n  return c.getRed() >= 0 && c.getRed() <= 255\n      && c.getGreen() >= 0 && c.getGreen() <= 255\n      && c.getBlue() >= 0 && c.getBlue() <= 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":["Avoid custom Color subclasses overriding channel getters.","Clamp channel values when computing colors dynamically.","Prefer String.format(\"#%02x%02x%02x\", ...) for HTML color conversion."],"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"}