material-components/material-components-android · error · IllegalArgumentException

Color names need to be at least four and correspond to attri

Error message

Color names need to be at least four and correspond to attribute resource ids.

What it means

Thrown by ColorGrid.createFromAttrResId(Context, String[], int[]). A ColorGrid is built from exactly four MaterialColorSpec entries, so the factory requires at least four color names and that the names array length matches the attr resource id array length. Mismatched or short arrays are rejected with IllegalArgumentException.

Source

Thrown at catalog/java/io/material/catalog/color/ColorGrid.java:58

    ColorRoleNames colorRoleNames = colorGridData.getColorRoleNames();
    MaterialColorSpec[] materialColorSpecs =
        new MaterialColorSpec[] {
          MaterialColorSpec.createFromColorValue(
              colorRoleNames.getAccentName(), colorRoles.getAccent()),
          MaterialColorSpec.createFromColorValue(
              colorRoleNames.getOnAccentName(), colorRoles.getOnAccent()),
          MaterialColorSpec.createFromColorValue(
              colorRoleNames.getAccentContainerName(), colorRoles.getAccentContainer()),
          MaterialColorSpec.createFromColorValue(
              colorRoleNames.getOnAccentContainerName(), colorRoles.getOnAccentContainer()),
        };
    return new ColorGrid(
        materialColorSpecs[0], materialColorSpecs[1], materialColorSpecs[2], materialColorSpecs[3]);
  }

  static ColorGrid createFromAttrResId(Context context, String[] colorNames, int[] attrResIds) {
    if (colorNames.length < 4 || colorNames.length != attrResIds.length) {
      throw new IllegalArgumentException(
          "Color names need to be at least four and correspond to attribute resource ids.");
    }
    return new ColorGrid(
        MaterialColorSpec.createFromAttrResId(context, colorNames[0], attrResIds[0]),
        MaterialColorSpec.createFromAttrResId(context, colorNames[1], attrResIds[1]),
        MaterialColorSpec.createFromAttrResId(context, colorNames[2], attrResIds[2]),
        MaterialColorSpec.createFromAttrResId(context, colorNames[3], attrResIds[3]));
  }

  private ColorGrid(
      @NonNull MaterialColorSpec materialColorSpecAccent,
      @NonNull MaterialColorSpec materialColorSpecOnAccent,
      @NonNull MaterialColorSpec materialColorSpecAccentContainer,
      @NonNull MaterialColorSpec materialColorSpecOnAccentContainer) {
    this.materialColorSpecAccent = materialColorSpecAccent;
    this.materialColorSpecOnAccent = materialColorSpecOnAccent;
    this.materialColorSpecAccentContainer = materialColorSpecAccentContainer;
    this.materialColorSpecOnAccentContainer = materialColorSpecOnAccentContainer;

View on GitHub (pinned to ac7e18efee)

Solutions

  1. Pass exactly four color names and four matching attribute resource ids, aligned by index.
  2. Add a precondition check on both arrays (length >= 4 and equal lengths) before calling the factory.
  3. Build both arrays from a single source of truth (e.g. one list of role->attr pairs mapped to two arrays) so they cannot diverge.

Example fix

// before
String[] names = {"Accent", "On Accent", "Accent Container"}; // only 3
ColorGrid grid = ColorGrid.createFromAttrResId(context, names, attrIds);

// after
String[] names = {"Accent", "On Accent", "Accent Container", "On Accent Container"};
int[] attrs = {attrAccent, attrOnAccent, attrAccentContainer, attrOnAccentContainer};
ColorGrid grid = ColorGrid.createFromAttrResId(context, names, attrs);
Defensive patterns

Strategy: validation

Validate before calling

if (colorNames.length >= 4 && attrResIds.length == colorNames.length) {
  ColorGrid grid = ColorGrid.createFromAttrResId(context, colorNames, attrResIds);
}

Prevention

When it happens

Trigger: Calling ColorGrid.createFromAttrResId with colorNames.length < 4, or colorNames.length != attrResIds.length (e.g. passing 3 names, or 4 names with 5 attr ids).

Common situations: Building a color role grid for a dynamic-color demo and forgetting one of the four roles (accent, onAccent, accentContainer, onAccentContainer); populating the two arrays in separate loops so their sizes drift; passing an empty attr id array during a refactor.

Related errors


AI-assisted analysis of material-components/material-components-android@ac7e18efee (2026-08-14). Data as JSON: /api/errors/9237f5e31c57fac8. Report an issue: GitHub.