stanfordnlp/CoreNLP · error · IllegalArgumentException

invalid hex color for green"+g

Error message

invalid hex color for green"+g

What it means

NERGUI.colorToHTML throws IllegalArgumentException when the green channel's hex string is longer than 2 characters. Like the red check, this is a defensive invariant that only fires for a Color with a green component outside 0-255 (possible only via a custom Color subclass or bad mock).

Solutions

  1. Use a standard java.awt.Color with all channels within 0-255.
  2. Validate channels before constructing the Color.
  3. Switch to String.format("#%02x%02x%02x", ...) which handles the formatting without throwing.

Example fix

// before
throw new IllegalArgumentException("invalid hex color for green"+g);
// after
g = g.substring(g.length()-2); // or String.format formatting
Defensive patterns

Strategy: type-guard

Validate before calling

static boolean isValidColor(Color c) {
  return c.getGreen() >= 0 && c.getGreen() <= 255 && c.getRed() >= 0 && c.getRed() <= 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 colorToHTML with a Color whose getGreen() returns a value whose hex string exceeds 2 chars, i.e. outside 0-255 — practically only a Color subclass or test stub.

Common situations: Test doubles with out-of-range channel values, or custom Color implementations computing channels incorrectly.

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/3426a0d36a3c8a20. Report an issue: GitHub.

Appendix: source

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

    }

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

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

View on GitHub (pinned to 1b7edd19c4)