{"record":{"id":"ca08c2f4d4cfb2aa","repo":"mozilla/pdf.js","slug":"field-numitems-is-read-only","errorCode":null,"errorMessage":"field.numItems is read-only","messagePattern":"field\\.numItems is read-only","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/scripting_api/field.js","lineNumber":176,"sourceCode":"    return this._charLimit;\n  }\n\n  set charLimit(limit) {\n    if (typeof limit !== \"number\") {\n      throw new Error(\"Invalid argument value\");\n    }\n    this._charLimit = Math.max(0, Math.floor(limit));\n  }\n\n  get numItems() {\n    if (!this._isChoice) {\n      throw new Error(\"Not a choice widget\");\n    }\n    return this._items.length;\n  }\n\n  set numItems(_) {\n    throw new Error(\"field.numItems is read-only\");\n  }\n\n  get strokeColor() {\n    return this._strokeColor;\n  }\n\n  set strokeColor(color) {\n    if (Color._isValidColor(color)) {\n      this._strokeColor = color;\n    }\n  }\n\n  get borderColor() {\n    return this.strokeColor;\n  }\n\n  set borderColor(color) {\n    this.strokeColor = color;","sourceCodeStart":158,"sourceCodeEnd":194,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/scripting_api/field.js#L158-L194","documentation":"Thrown by the setter of `Field.numItems` (src/scripting_api/field.js:176). The item count is derived from the underlying `_items` array and cannot be set directly; to change it you must use `setItems`, `insertItemAt`, `deleteItemAt`, or `clearItems`.","triggerScenarios":"Executing `f.numItems = 5;` to try to resize a list. Most often a copy/paste mistake or generic 'reset all properties' logic that round-trips the current value back through the setter.","commonSituations":"Form-state cloning code; scripts written for an older reader that tolerated the assignment; mis-typed `f.numItems == 5` (== vs =) silently assigning.","solutions":["Replace the assignment with a call to `f.setItems(newArray)` whose length gives the desired count.","Remove the assignment entirely if it was unintended.","Wrap in try/catch if the write comes from generic serialization code you cannot refactor."],"exampleFix":"// before\nf.numItems = newValues.length;\n// after\nf.setItems(newValues); // numItems follows from the array length","handlingStrategy":"try-catch","validationCode":"const READ_ONLY_FIELD_PROPS = new Set(['page','numItems']);\nfunction isWritableFieldProp(key) {\n  return !READ_ONLY_FIELD_PROPS.has(key);\n}","typeGuard":null,"tryCatchPattern":"try {\n  f.numItems = n;\n} catch (e) {\n  // numItems is read-only; rebuild via setItems instead\n  f.setItems(new Array(n).fill(''));\n}","preventionTips":["Use setItems/insertItemAt/deleteItemAt/clearItems to change item count, never assignment.","Blocklist read-only field properties in serialization/restore loops.","Watch for accidental '=' where '==' was intended."],"tags":["scripting-api","field","read-only","choice-widget","acrobat-js"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}