{"record":{"id":"1e1ada79d14512aa","repo":"karatelabs/karate","slug":"tobytes-list-must-contain-only-numbers-got-classname-at","errorCode":null,"errorMessage":"toBytes() list must contain only numbers, got: {className} at index {i}","messagePattern":"toBytes\\(\\) list must contain only numbers, got: (.+?) at index (.+?)","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java","lineNumber":575,"sourceCode":"        return args -> {\n            if (args.length < 1) {\n                throw new RuntimeException(\"toBytes() needs one argument: a list of numbers\");\n            }\n            Object arg = args[0];\n            if (arg instanceof byte[]) {\n                return arg; // already bytes\n            }\n            if (!(arg instanceof List)) {\n                throw new RuntimeException(\"toBytes() argument must be a list of numbers, got: \" + arg.getClass().getName());\n            }\n            List<Object> list = (List<Object>) arg;\n            byte[] bytes = new byte[list.size()];\n            for (int i = 0; i < list.size(); i++) {\n                Object item = list.get(i);\n                if (item instanceof Number num) {\n                    bytes[i] = num.byteValue();\n                } else {\n                    throw new RuntimeException(\"toBytes() list must contain only numbers, got: \" + item.getClass().getName() + \" at index \" + i);\n                }\n            }\n            return bytes;\n        };\n    }\n\n    static JavaInvokable toJson() {\n        return args -> {\n            if (args.length < 1) {\n                throw new RuntimeException(\"toJson() needs at least one argument\");\n            }\n            Object obj = args[0];\n            boolean removeNulls = args.length > 1 && Boolean.TRUE.equals(args[1]);\n            Object result = Json.of(obj).value();\n            if (removeNulls) {\n                removeNullValues(result);\n            }\n            return result;","sourceCodeStart":557,"sourceCodeEnd":593,"githubUrl":"https://github.com/karatelabs/karate/blob/a22eb90246d958d15a47bf436693d0121ad2812d/karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java#L557-L593","documentation":"karate.toBytes() converts each element of the supplied List to a byte via Number.byteValue(). If any element is not a Number, the library throws this error naming the element's Java class and its index in the list, aborting the conversion. This is the per-element guard inside the conversion loop.","triggerScenarios":"karate.toBytes([72, 'a', 108]) — a string, null, boolean, or nested list/map inside the numbers list; values parsed from JSON where some entries came back as strings.","commonSituations":"Building byte payloads from data fetched from APIs or CSV where a column was parsed as text; nulls sneaking into generated lists; values outside 0-255 are NOT caught here (they are truncated via byteValue), only non-numeric types are.","solutions":["Ensure every list element is a number; fix or filter out nulls and non-numeric entries.","Parse string numbers explicitly before passing (e.g. with JS Number()/parseInt).","Sanitize data from external sources: coerce to numbers before building the byte list.","Note the offending index in the message to locate the bad element quickly."],"exampleFix":"// before\nkarate.toBytes([72, '105', null])\n// after\nkarate.toBytes([72, 105, 0])","handlingStrategy":"validation","validationCode":"for (var i = 0; i < nums.length; i++) { if (typeof nums[i] !== 'number' || nums[i] == null) { throw new Error('non-number at index ' + i + ': ' + nums[i]) } }","typeGuard":"function allNumbers(list) { return Array.isArray(list) && list.every(function (n) { return typeof n === 'number' }) }","tryCatchPattern":"try { var bytes = karate.toBytes(nums) } catch (e) { var m = ('' + e).match(/got: (\\S+) at index (\\d+)/); if (m) { karate.logger.warn('bad element at index ' + m[2] + ': ' + m[1]); bytes = null } throw e }","preventionTips":["Sanitize external data (API/CSV) into numbers before building byte lists","Filter out nulls and non-numeric entries before calling toBytes","Remember values are truncated via byteValue — pre-check the 0-255 range if overflow matters"],"tags":["javascript","argument-validation","bytes","type-error"],"backgroundTag":"invalid-argument-value","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"}