stanfordnlp/CoreNLP · error · IllegalArgumentException

invalid hex color for red"+r

Error message

invalid hex color for red"+r

What it means

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.

Solutions

  1. Ensure the Color argument is a standard java.awt.Color constructed with channel values in 0-255.
  2. Clamp or validate channel values before constructing the Color.
  3. Replace with String.format("#%02x%02x%02x", r, g, b) which never throws for such ranges.

Example fix

// before
throw new IllegalArgumentException("invalid hex color for red"+r);
// after
r = r.substring(r.length()-2); // or use String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue());
Defensive patterns

Strategy: type-guard

Validate before calling

static boolean isValidColor(Color c) {
  return c.getRed() >= 0 && c.getRed() <= 255
      && c.getGreen() >= 0 && c.getGreen() <= 255
      && c.getBlue() >= 0 && c.getBlue() <= 255;
}

Type guard

static boolean isStandardColor(Color c) { return c.getClass() == java.awt.Color.class; }

Try / catch

try {
  String html = NERGUI.colorToHTML(color);
} catch (IllegalArgumentException e) {
  String html = String.format("#%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue());
}

Prevention

When it happens

Trigger: 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.

Common situations: Passing a mock/stub Color with out-of-range channel values in tests, or a Color subclass returning values outside 0-255.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/0b3787cf82538013. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/ie/NERGUI.java:588

    editorPane.setDocument(doc);
    try {
      doc.insertString(0, initText, defaultAttrSet);
    } catch (Exception ex) {
      throw new RuntimeException(ex);
    }

    JScrollPane scrollPane = new JScrollPane(editorPane);
    frame.getContentPane().add(scrollPane, BorderLayout.CENTER);

    editorPane.setEditable(true);

  }

  public static String colorToHTML(Color color) {
    String r = Integer.toHexString(color.getRed());
    if (r.length() == 0) { r = "00"; }
    else if (r.length() == 1) { r = "0" + r; }
    else if (r.length() > 2) { throw new IllegalArgumentException("invalid hex color for red"+r); }

    String g = Integer.toHexString(color.getGreen());
    if (g.length() == 0) { g = "00"; }
    else if (g.length() == 1) { g = "0" + g; }
    else if (g.length() > 2) { throw new IllegalArgumentException("invalid hex color for green"+g); }

    String b = Integer.toHexString(color.getBlue());
    if (b.length() == 0) { b = "00"; }
    else if (b.length() == 1) { b = "0" + b; }
    else if (b.length() > 2) { throw new IllegalArgumentException("invalid hex color for blue"+b); }

    return "#"+r+g+b;
  }

  static class ColorIcon implements Icon {
    Color color;

    public ColorIcon(Color c) {

View on GitHub (pinned to 1b7edd19c4)