{"record":{"id":"a42221eed58684cc","repo":"zxing/zxing","slug":"illegal-value-s-for-layers","errorCode":null,"errorMessage":"Illegal value %s for layers","messagePattern":"Illegal value (.+?) for layers","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/com/google/zxing/aztec/encoder/Encoder.java","lineNumber":137,"sourceCode":"   * @return Aztec symbol matrix with metadata\n   */\n  public static AztecCode encode(byte[] data, int minECCPercent, int userSpecifiedLayers, Charset charset) {\n    // High-level encode\n    BitArray bits = new HighLevelEncoder(data, charset).encode();\n\n    // stuff bits and choose symbol size\n    int eccBits = bits.getSize() * minECCPercent / 100 + 11;\n    int totalSizeBits = bits.getSize() + eccBits;\n    boolean compact;\n    int layers;\n    int totalBitsInLayer;\n    int wordSize;\n    BitArray stuffedBits;\n    if (userSpecifiedLayers != DEFAULT_AZTEC_LAYERS) {\n      compact = userSpecifiedLayers < 0;\n      layers = Math.abs(userSpecifiedLayers);\n      if (layers > (compact ? MAX_NB_BITS_COMPACT : MAX_NB_BITS)) {\n        throw new IllegalArgumentException(\n            String.format(\"Illegal value %s for layers\", userSpecifiedLayers));\n      }\n      totalBitsInLayer = totalBitsInLayer(layers, compact);\n      wordSize = WORD_SIZE[layers];\n      int usableBitsInLayers = totalBitsInLayer - (totalBitsInLayer % wordSize);\n      stuffedBits = stuffBits(bits, wordSize);\n      if (stuffedBits.getSize() + eccBits > usableBitsInLayers) {\n        throw new IllegalArgumentException(\"Data to large for user specified layer\");\n      }\n      if (compact && stuffedBits.getSize() > wordSize * 64) {\n        // Compact format only allows 64 data words, though C4 can hold more words than that\n        throw new IllegalArgumentException(\"Data to large for user specified layer\");\n      }\n    } else {\n      wordSize = 0;\n      stuffedBits = null;\n      // We look at the possible table sizes in the order Compact1, Compact2, Compact3,\n      // Compact4, Normal4,...  Normal(i) for i < 4 isn't typically used since Compact(i+1)","sourceCodeStart":119,"sourceCodeEnd":155,"githubUrl":"https://github.com/zxing/zxing/blob/19aa2d8254410e161f04dc3c928e68d5e90233c2/core/src/main/java/com/google/zxing/aztec/encoder/Encoder.java#L119-L155","documentation":"Thrown by Encoder.encode when userSpecifiedLayers is non-zero and its absolute value exceeds the maximum: 4 for compact mode (negative userSpecifiedLayers) or 32 for full-size mode (positive userSpecifiedLayers). The layer count is out of the valid range. Note: negative values select compact mode, positive values select full-size mode.","triggerScenarios":"Encoder.encode(data, minECCPercent, userSpecifiedLayers) is called with |userSpecifiedLayers| > 4 (compact) or > 32 (full-size). For example, userSpecifiedLayers = -5 (compact mode, 5 layers > MAX_NB_BITS_COMPACT=4) or userSpecifiedLayers = 33 (full-size, 33 > MAX_NB_BITS=32).","commonSituations":"Passing a layer count derived from user input without clamping to the valid range. A copy-paste error using the wrong constant. Misunderstanding the sign convention (negative = compact). Passing 0 to mean '1 layer' when 0 actually means auto-size.","solutions":["Clamp userSpecifiedLayers to the valid range: [-4, -1] for compact, [1, 32] for full-size, or 0 for auto.","Pass DEFAULT_AZTEC_LAYERS (0) to let the encoder auto-select the optimal layer count.","Validate the layer parameter against MAX_NB_BITS (32) and MAX_NB_BITS_COMPACT (4) before calling encode.","Remember the sign convention: negative = compact, positive = full-size."],"exampleFix":"// before\nAztecCode code = Encoder.encode(data, 33, -5, charset); // exceeds limits\n\n// after\n// Let the encoder auto-select layers (recommended)\nAztecCode code = Encoder.encode(data, 33, Encoder.DEFAULT_AZTEC_LAYERS, charset);\n\n// or validate explicitly\nint layers = userSpecifiedLayers;\nboolean compact = layers < 0;\nint maxLayers = compact ? 4 : 32;\nif (Math.abs(layers) > maxLayers) {\n  throw new IllegalArgumentException(\"Layers must be within [\" + (compact ? -4 : 1) + \",\" + maxLayers + \"]\");\n}\nAztecCode code = Encoder.encode(data, 33, layers, charset);","handlingStrategy":"validation","validationCode":"// Validate layer count before encoding\nboolean compact = userSpecifiedLayers < 0;\nint maxLayers = compact ? 4 : 32;\nif (userSpecifiedLayers != 0 && Math.abs(userSpecifiedLayers) > maxLayers) {\n  throw new IllegalArgumentException(\n    \"Layers must be 0 (auto), [-4,-1] compact, or [1,32] full-size\");\n}","typeGuard":"boolean isValidLayers(int layers) {\n  if (layers == 0) return true; // auto\n  boolean compact = layers < 0;\n  int max = compact ? 4 : 32;\n  return Math.abs(layers) <= max;\n}","tryCatchPattern":"try {\n  AztecCode code = Encoder.encode(data, minECC, userSpecifiedLayers, charset);\n} catch (IllegalArgumentException e) {\n  if (e.getMessage().startsWith(\"Illegal value\")) {\n    // Re-run with auto-sizing\n    code = Encoder.encode(data, minECC, Encoder.DEFAULT_AZTEC_LAYERS, charset);\n  } else {\n    throw e;\n  }\n}","preventionTips":["Use DEFAULT_AZTEC_LAYERS (0) unless you need a specific symbol size.","Validate layer count: compact range is [-4,-1], full-size range is [1,32].","Remember the sign convention: negative = compact, positive = full-size."],"tags":["aztec","encode","validation","illegal-argument","layers"],"backgroundTag":null,"analyzedSha":"19aa2d8254410e161f04dc3c928e68d5e90233c2","analyzedAt":"2026-08-14T03:07:53.428Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}