{"record":{"id":"1302f1c3b13721fc","repo":"zxing/zxing","slug":"the-number-of-codewords-does-not-match-the-selecte","errorCode":null,"errorMessage":"The number of codewords does not match the selected symbol","messagePattern":"The number of codewords does not match the selected symbol","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/com/google/zxing/datamatrix/encoder/ErrorCorrection.java","lineNumber":103,"sourceCode":"      if (p >= 256) {\n        p ^= MODULO_VALUE;\n      }\n    }\n  }\n\n  private ErrorCorrection() {\n  }\n\n  /**\n   * Creates the ECC200 error correction for an encoded message.\n   *\n   * @param codewords  the codewords\n   * @param symbolInfo information about the symbol to be encoded\n   * @return the codewords with interleaved error correction.\n   */\n  public static String encodeECC200(String codewords, SymbolInfo symbolInfo) {\n    if (codewords.length() != symbolInfo.getDataCapacity()) {\n      throw new IllegalArgumentException(\n          \"The number of codewords does not match the selected symbol\");\n    }\n    StringBuilder sb = new StringBuilder(symbolInfo.getDataCapacity() + symbolInfo.getErrorCodewords());\n    sb.append(codewords);\n    int blockCount = symbolInfo.getInterleavedBlockCount();\n    if (blockCount == 1) {\n      String ecc = createECCBlock(codewords, symbolInfo.getErrorCodewords());\n      sb.append(ecc);\n    } else {\n      sb.setLength(sb.capacity());\n      int[] dataSizes = new int[blockCount];\n      int[] errorSizes = new int[blockCount];\n      for (int i = 0; i < blockCount; i++) {\n        dataSizes[i] = symbolInfo.getDataLengthForInterleavedBlock(i + 1);\n        errorSizes[i] = symbolInfo.getErrorLengthForInterleavedBlock(i + 1);\n      }\n      for (int block = 0; block < blockCount; block++) {\n        StringBuilder temp = new StringBuilder(dataSizes[block]);","sourceCodeStart":85,"sourceCodeEnd":121,"githubUrl":"https://github.com/zxing/zxing/blob/19aa2d8254410e161f04dc3c928e68d5e90233c2/core/src/main/java/com/google/zxing/datamatrix/encoder/ErrorCorrection.java#L85-L121","documentation":"Thrown by ErrorCorrection.encodeECC200(String, SymbolInfo) when codewords.length() != symbolInfo.getDataCapacity(). ECC200 error correction is computed per symbol with a fixed data capacity; the input string must already be padded to exactly that length. The mismatch means the codeword stream was built against a different symbol than the one supplied.","triggerScenarios":"Calling encodeECC200 with a codeword string whose length differs from the selected SymbolInfo's data capacity, e.g. picking a symbol after the codewords were already generated for another size, or forgetting to pad.","commonSituations":"Mixing symbol selection and codeword generation steps, or custom Data Matrix pipelines that change SymbolInfo between padding and ECC. The public DataMatrixWriter handles this correctly internally, so this is a custom-pipeline error.","solutions":["Re-lookup SymbolInfo for the final codeword count and pad to its data capacity before calling encodeECC200.","Pass the same SymbolInfo instance used to compute capacity into encodeECC200.","Add an assertion codewords.length() == symbolInfo.getDataCapacity() before the call."],"exampleFix":"// before\nString ecc = ErrorCorrection.encodeECC200(codewords, someSymbol);\n// after\nSymbolInfo si = SymbolInfo.lookup(codewords.length()); // or the one used to build it\nwhile (codewords.length() < si.getDataCapacity()) codewords.append((char) 129); // pad\nString ecc = ErrorCorrection.encodeECC200(codewords, si);","handlingStrategy":"validation","validationCode":"if (codewords.length() != symbolInfo.getDataCapacity()) {\n  throw new IllegalArgumentException(\"codewords=\" + codewords.length()\n    + \" but symbol dataCapacity=\" + symbolInfo.getDataCapacity());\n}\nErrorCorrection.encodeECC200(codewords, symbolInfo);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Pass the same SymbolInfo used to build the codewords into encodeECC200.","Pad codewords to exactly getDataCapacity() before the call.","Add a length assertion at every encodeECC200 call site."],"tags":["zxing","datamatrix","error-correction","precondition"],"backgroundTag":null,"analyzedSha":"19aa2d8254410e161f04dc3c928e68d5e90233c2","analyzedAt":"2026-08-14T03:07:53.428Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}