{"record":{"id":"65ece8f0fb3079d4","repo":"karatelabs/karate","slug":"appendto-needs-at-least-two-arguments-list-and-item-s","errorCode":null,"errorMessage":"appendTo() needs at least two arguments: list and item(s)","messagePattern":"appendTo\\(\\) needs at least two arguments: list and item\\(s\\)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":113,"sourceCode":"                result.add(first);\n            }\n            for (int i = 1; i < args.length; i++) {\n                Object item = args[i];\n                if (item instanceof List) {\n                    result.addAll((List<?>) item);\n                } else {\n                    result.add(item);\n                }\n            }\n            return result;\n        };\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    static JavaInvokable appendTo() {\n        return args -> {\n            if (args.length < 2) {\n                throw new RuntimeException(\"appendTo() needs at least two arguments: list and item(s)\");\n            }\n            List<Object> list = (List<Object>) args[0];\n            for (int i = 1; i < args.length; i++) {\n                Object item = args[i];\n                if (item instanceof List) {\n                    list.addAll((List<?>) item);\n                } else {\n                    list.add(item);\n                }\n            }\n            return list;\n        };\n    }\n\n    /**\n     * Remove duplicates from a list while preserving order.\n     * Usage: karate.distinct([1, 2, 2, 3, 1]) => [1, 2, 3]\n     */","sourceCodeStart":95,"sourceCodeEnd":131,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L95-L131","documentation":"Karate's appendTo() JS utility mutates a list in place by adding one or more items. It requires at least two arguments: the target list plus at least one item to append. Fewer arguments throw this error.","triggerScenarios":"Calling appendTo() with zero arguments or with only the list (appendTo(list)) and no items.","commonSituations":"Loop code that conditionally adds items but passes none on the first iteration; mixing up appendTo with append (which returns a new list); forgetting to pass items after refactoring.","solutions":["Provide at least one item: appendTo(list, item)","Append multiple items by passing them together: appendTo(list, a, b)","To flatten a list of items, pass the list as an item so it is addAll'd, or loop appendTo calls"],"exampleFix":"// before\nkarate.appendTo(list);\n// after\nkarate.appendTo(list, 'newItem');","handlingStrategy":"validation","validationCode":"if (!list || items.length === 0) throw new Error('appendTo needs a list and at least one item');","typeGuard":"function canAppendTo(args) { return Array.isArray(args) && args.length >= 2 && Array.isArray(args[0]); }","tryCatchPattern":"try { karate.appendTo(list, item); } catch (e) { karate.log('appendTo requires list + item: ' + e.message); }","preventionTips":["Guard conditional append loops so at least one item is always passed","Distinguish append (new list) from appendTo (mutates)","Ensure the target variable is an initialized array/list"],"tags":["argument-count","javascript","karate","list"],"backgroundTag":"missing-required-argument","analyzedSha":"a22eb90246d958d15a47bf436693d0121ad2812d","analyzedAt":"2026-09-12T09:01:00.220Z","contentChangedAt":"2026-09-12T09:01:00.220Z","schemaVersion":2},"datasetVersion":"2026-09-16T19:17:19.609Z"}