{"record":{"id":"9237f5e31c57fac8","repo":"material-components/material-components-android","slug":"color-names-need-to-be-at-least-four-and-correspon","errorCode":null,"errorMessage":"Color names need to be at least four and correspond to attribute resource ids.","messagePattern":"Color names need to be at least four and correspond to attribute resource ids\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"catalog/java/io/material/catalog/color/ColorGrid.java","lineNumber":58,"sourceCode":"    ColorRoleNames colorRoleNames = colorGridData.getColorRoleNames();\n    MaterialColorSpec[] materialColorSpecs =\n        new MaterialColorSpec[] {\n          MaterialColorSpec.createFromColorValue(\n              colorRoleNames.getAccentName(), colorRoles.getAccent()),\n          MaterialColorSpec.createFromColorValue(\n              colorRoleNames.getOnAccentName(), colorRoles.getOnAccent()),\n          MaterialColorSpec.createFromColorValue(\n              colorRoleNames.getAccentContainerName(), colorRoles.getAccentContainer()),\n          MaterialColorSpec.createFromColorValue(\n              colorRoleNames.getOnAccentContainerName(), colorRoles.getOnAccentContainer()),\n        };\n    return new ColorGrid(\n        materialColorSpecs[0], materialColorSpecs[1], materialColorSpecs[2], materialColorSpecs[3]);\n  }\n\n  static ColorGrid createFromAttrResId(Context context, String[] colorNames, int[] attrResIds) {\n    if (colorNames.length < 4 || colorNames.length != attrResIds.length) {\n      throw new IllegalArgumentException(\n          \"Color names need to be at least four and correspond to attribute resource ids.\");\n    }\n    return new ColorGrid(\n        MaterialColorSpec.createFromAttrResId(context, colorNames[0], attrResIds[0]),\n        MaterialColorSpec.createFromAttrResId(context, colorNames[1], attrResIds[1]),\n        MaterialColorSpec.createFromAttrResId(context, colorNames[2], attrResIds[2]),\n        MaterialColorSpec.createFromAttrResId(context, colorNames[3], attrResIds[3]));\n  }\n\n  private ColorGrid(\n      @NonNull MaterialColorSpec materialColorSpecAccent,\n      @NonNull MaterialColorSpec materialColorSpecOnAccent,\n      @NonNull MaterialColorSpec materialColorSpecAccentContainer,\n      @NonNull MaterialColorSpec materialColorSpecOnAccentContainer) {\n    this.materialColorSpecAccent = materialColorSpecAccent;\n    this.materialColorSpecOnAccent = materialColorSpecOnAccent;\n    this.materialColorSpecAccentContainer = materialColorSpecAccentContainer;\n    this.materialColorSpecOnAccentContainer = materialColorSpecOnAccentContainer;","sourceCodeStart":40,"sourceCodeEnd":76,"githubUrl":"https://github.com/material-components/material-components-android/blob/ac7e18efeefb331850c561faf9ab8bf81d27ba68/catalog/java/io/material/catalog/color/ColorGrid.java#L40-L76","documentation":"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.","triggerScenarios":"Calling ColorGrid.createFromAttrResId with colorNames.length < 4, or colorNames.length != attrResIds.length (e.g. passing 3 names, or 4 names with 5 attr ids).","commonSituations":"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.","solutions":["Pass exactly four color names and four matching attribute resource ids, aligned by index.","Add a precondition check on both arrays (length >= 4 and equal lengths) before calling the factory.","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."],"exampleFix":"// before\nString[] names = {\"Accent\", \"On Accent\", \"Accent Container\"}; // only 3\nColorGrid grid = ColorGrid.createFromAttrResId(context, names, attrIds);\n\n// after\nString[] names = {\"Accent\", \"On Accent\", \"Accent Container\", \"On Accent Container\"};\nint[] attrs = {attrAccent, attrOnAccent, attrAccentContainer, attrOnAccentContainer};\nColorGrid grid = ColorGrid.createFromAttrResId(context, names, attrs);","handlingStrategy":"validation","validationCode":"if (colorNames.length >= 4 && attrResIds.length == colorNames.length) {\n  ColorGrid grid = ColorGrid.createFromAttrResId(context, colorNames, attrResIds);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Derive both arrays from one list of (name, attr) pairs so lengths always match.","Unit-test the grid builder with a 4-role input to catch drift early."],"tags":["catalog-demo","color-roles","illegal-argument","validation"],"backgroundTag":null,"analyzedSha":"ac7e18efeefb331850c561faf9ab8bf81d27ba68","analyzedAt":"2026-08-14T15:35:35.735Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}