{"record":{"id":"05bb5bd27def4e9a","repo":"mozilla/pdf.js","slug":"not-a-choice-widget","errorCode":null,"errorMessage":"Not a choice widget","messagePattern":"Not a choice widget","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"src/scripting_api/field.js","lineNumber":170,"sourceCode":"\n  set bgColor(color) {\n    this.fillColor = color;\n  }\n\n  get charLimit() {\n    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","sourceCodeStart":152,"sourceCodeEnd":188,"githubUrl":"https://github.com/mozilla/pdf.js/blob/5903d58d58e4dd9ce6ffa3834aea8480f06b4ada/src/scripting_api/field.js#L152-L188","documentation":"Thrown by the getter of `Field.numItems` (src/scripting_api/field.js:170). `numItems` is only defined for choice widgets (combo boxes and list boxes). PDF.js marks a field as a choice via `_isChoice = Array.isArray(data.items)` at construction; accessing `numItems` on a text/button/signature field throws.","triggerScenarios":"Calling `var n = someTextField.numItems;` on a non-choice field, or generic enumeration code that reads `numItems` on every field in `doc.getArray()` / `doc.numFields` iteration regardless of `field.type`.","commonSituations":"Bulk form-introspection code that assumes all fields support item lists; scripts that were written for a list-box-only form and later run over a mixed form.","solutions":["Gate access on the field type: only read `numItems` when `field.type === 'ListBox' || field.type === 'ComboBox'`.","Check `'numItems' in field` is not sufficient (the accessor always exists); instead branch on `field.type`.","Wrap the access in try/catch if you must iterate heterogeneous fields generically."],"exampleFix":"// before\nfor (let k = 0; k < f.numItems; k++) { ... }\n// after\nif (f.type === 'ListBox' || f.type === 'ComboBox') {\n  for (let k = 0; k < f.numItems; k++) { ... }\n}","handlingStrategy":"validation","validationCode":"function isChoiceField(f) {\n  return f && (f.type === 'ListBox' || f.type === 'ComboBox');\n}\nfunction numItemsSafe(f) {\n  return isChoiceField(f) ? f.numItems : 0;\n}","typeGuard":"function isChoiceField(f) {\n  return !!f && (f.type === 'ListBox' || f.type === 'ComboBox');\n}","tryCatchPattern":null,"preventionTips":["Branch on field.type before reading numItems; do not rely on 'in'.","In bulk iteration, separate choice fields from the rest first.","Treat numItems absence as 'single-value field' rather than erroring."],"tags":["scripting-api","field","choice-widget","acrobat-js"],"backgroundTag":null,"analyzedSha":"5903d58d58e4dd9ce6ffa3834aea8480f06b4ada","analyzedAt":"2026-08-13T02:28:27.364Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}