karatelabs/karate · error · RuntimeException
toBytes() list must contain only numbers, got
Error message
toBytes() list must contain only numbers, got: {className} at index {i} What it means
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.
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.
Example fix
// before karate.toBytes([72, '105', null]) // after karate.toBytes([72, 105, 0])
Defensive patterns
Strategy: validation
Validate before calling
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]) } } Type guard
function allNumbers(list) { return Array.isArray(list) && list.every(function (n) { return typeof n === 'number' }) } Try / catch
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 } Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.
Related errors
- toBytes() argument must be a list of numbers, got
- toBytes() needs one argument: a list of numbers
- xmlPath() first argument must be XML node or string, but was
- read() needs at least one argument
- sysenv() needs the environment-variable name
AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12).
Data as JSON: /api/errors/1e1ada79d14512aa.
Report an issue: GitHub.
Appendix: source
Thrown at karate-core/src/main/java/io/karatelabs/core/KarateJsUtils.java:575
return args -> {
if (args.length < 1) {
throw new RuntimeException("toBytes() needs one argument: a list of numbers");
}
Object arg = args[0];
if (arg instanceof byte[]) {
return arg; // already bytes
}
if (!(arg instanceof List)) {
throw new RuntimeException("toBytes() argument must be a list of numbers, got: " + arg.getClass().getName());
}
List<Object> list = (List<Object>) arg;
byte[] bytes = new byte[list.size()];
for (int i = 0; i < list.size(); i++) {
Object item = list.get(i);
if (item instanceof Number num) {
bytes[i] = num.byteValue();
} else {
throw new RuntimeException("toBytes() list must contain only numbers, got: " + item.getClass().getName() + " at index " + i);
}
}
return bytes;
};
}
static JavaInvokable toJson() {
return args -> {
if (args.length < 1) {
throw new RuntimeException("toJson() needs at least one argument");
}
Object obj = args[0];
boolean removeNulls = args.length > 1 && Boolean.TRUE.equals(args[1]);
Object result = Json.of(obj).value();
if (removeNulls) {
removeNullValues(result);
}
return result;View on GitHub (pinned to a22eb90246)