stanfordnlp/CoreNLP · error · IllegalArgumentException

invalid hex color for blue"+b

Error message

invalid hex color for blue"+b

What it means

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.

Solutions

  1. Use standard Color instances with valid 0-255 channels.
  2. Clamp channel values before constructing the Color.
  3. Use String.format("#%02x%02x%02x", ...) instead of manual hex conversion.

Example fix

// before
throw new IllegalArgumentException("invalid hex color for blue"+b);
// after
return 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.getBlue() >= 0 && c.getBlue() <= 255 && c.getRed() >= 0 && c.getRed() <= 255 && c.getGreen() >= 0 && c.getGreen() <= 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 colorToHTML with a Color whose getBlue() returns a value outside 0-255 (custom subclass or stub).

Common situations: Mocks in unit tests, custom Color subclasses with computed channel values.

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/69712d418b061759. Report an issue: GitHub.

Appendix: source

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

    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) {
      color = c;
    }

    public void paintIcon(Component c, Graphics g, int x, int y) {
      g.setColor(color);
      g.fillRect(x, y, getIconWidth(), getIconHeight());
    }

    public int getIconWidth() {
      return 10;

View on GitHub (pinned to 1b7edd19c4)