{"record":{"id":"3ccf4ba0490a5116","repo":"junit-team/junit5","slug":"argument-at-index-d-with-value-s-and-type","errorCode":null,"errorMessage":"Argument at index [%d] with value [%s] and type [%s] could not be converted or cast to type [%s].","messagePattern":"Argument at index \\[(.+?)\\] with value \\[(.+?)\\] and type \\[(.+?)\\] could not be converted or cast to type \\[(.+?)\\]\\.","errorType":"exception","errorClass":"ArgumentAccessException","httpStatus":null,"severity":"error","filePath":"junit-jupiter-params/src/main/java/org/junit/jupiter/params/aggregator/DefaultArgumentsAccessor.java","lineNumber":81,"sourceCode":"\tpublic @Nullable Object get(int index) {\n\t\tPreconditions.condition(index >= 0 && index < this.arguments.length,\n\t\t\t() -> \"index must be >= 0 and < %d\".formatted(this.arguments.length));\n\t\treturn this.arguments[index];\n\t}\n\n\t@Override\n\tpublic <T> @Nullable T get(int index, Class<T> requiredType) {\n\t\tPreconditions.notNull(requiredType, \"requiredType must not be null\");\n\t\tObject value = get(index);\n\t\ttry {\n\t\t\tObject convertedValue = converter.apply(value, requiredType);\n\t\t\treturn requiredType.cast(convertedValue);\n\t\t}\n\t\tcatch (Exception ex) {\n\t\t\tString message = \"Argument at index [%d] with value [%s] and type [%s] could not be converted or cast to type [%s].\".formatted(\n\t\t\t\tindex, value, ClassUtils.nullSafeToString(value == null ? null : value.getClass()),\n\t\t\t\trequiredType.getName());\n\t\t\tthrow new ArgumentAccessException(message, ex);\n\t\t}\n\t}\n\n\t@Override\n\tpublic @Nullable Character getCharacter(int index) {\n\t\treturn get(index, Character.class);\n\t}\n\n\t@Override\n\tpublic @Nullable Boolean getBoolean(int index) {\n\t\treturn get(index, Boolean.class);\n\t}\n\n\t@Override\n\tpublic @Nullable Byte getByte(int index) {\n\t\treturn get(index, Byte.class);\n\t}\n","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/junit-team/junit5/blob/f070c699a08b5d8393df9afd147af5c5e90bb21b/junit-jupiter-params/src/main/java/org/junit/jupiter/params/aggregator/DefaultArgumentsAccessor.java#L63-L99","documentation":"Thrown by DefaultArgumentsAccessor.get(int index, Class<T> requiredType) when the value at the given index cannot be converted to the requested type via the DefaultArgumentConverter, or the converted value fails a Class.cast(). The message includes the index, the actual value (toString), its type, and the target type. The original conversion exception is attached as the cause.","triggerScenarios":"Call accessor.getInteger(0) on a CSV row where the first column is a non-numeric string. Call accessor.getString(0) where the value is a complex object that can't be string-converted by the built-in converter. The converter returns a type that is not assignable to requiredType, causing ClassCastException inside the cast().","commonSituations":"CSV-based parameterized tests where a column contains unexpected data (e.g., 'N/A' in a numeric column). Type mismatches between the declared parameter type and the actual argument value type. Index errors where the wrong column index is accessed. Argument values from custom ArgumentsProvider that aren't strings and don't match the accessor's expected type.","solutions":["Check the actual value and type shown in the error message against the requested type to identify the mismatch","Ensure the CSV/source data in the failing row matches the expected type, or add null/empty checks","Use a custom ArgumentConverter via @ConvertWith to handle non-standard conversions","Verify the index used in accessor.getX(index) matches the actual column order in the source data"],"exampleFix":"// before — accessor type mismatch on CSV data\n@ParameterizedTest\n@CsvSource({ \"name,abc\", \"age,xyz\" })\nvoid test(String label, @AggregateWith(MyAggregator.class) Data data) { }\n\n// In aggregator: accessor.getInteger(1) on \"abc\" → error\n// after — validate before converting or fix the source data\n@ParameterizedTest\n@CsvSource({ \"name,42\", \"age,99\" })\nvoid test(String label, @AggregateWith(MyAggregator.class) Data data) { }\n\n// Or in the aggregator, handle the type gracefully:\nObject raw = accessor.get(1);\nint value = (raw instanceof Number n) ? n.intValue() : Integer.parseInt(raw.toString());","handlingStrategy":"try-catch","validationCode":"// Validate argument type before calling accessor.getX(index)\nimport org.junit.jupiter.params.aggregator.ArgumentsAccessor;\n\nstatic Integer safeGetInteger(ArgumentsAccessor accessor, int index) {\n    Object value = accessor.get(index);\n    if (value == null) return null;\n    if (value instanceof Number n) return n.intValue();\n    if (value instanceof String s) {\n        try { return Integer.parseInt(s.trim()); }\n        catch (NumberFormatException e) { return null; }\n    }\n    return null;\n}","typeGuard":"// Type guard for checking if an accessor value can be safely cast\nstatic boolean isConvertibleTo(ArgumentsAccessor accessor, int index, Class<?> target) {\n    Object value = accessor.get(index);\n    if (value == null) return !target.isPrimitive();\n    if (target.isInstance(value)) return true;\n    if (value instanceof String s) {\n        try { DefaultArgumentConverter.INSTANCE.convert(s, target, ClassLoader.getSystemClassLoader()); return true; }\n        catch (Exception e) { return false; }\n    }\n    return false;\n}","tryCatchPattern":"// Wrap accessor type access in try-catch for robust error handling\ntry {\n    int value = accessor.getInteger(1);\n} catch (ArgumentAccessException e) {\n    // Log the index, value, and expected type from the error message\n    logger.warning(\"Failed to get integer at index 1: \" + e.getMessage());\n    // Use a default or skip this invocation\n    org.junit.jupiter.api.Assumptions.assumeTrue(false, \"Skipping: invalid argument at index 1\");\n}","preventionTips":["Ensure CSV/source data types match the accessor method types (getInteger for numeric columns, getString for text columns)","Validate source data format before feeding it to parameterized tests — run schema checks on CSV files","Use accessor.get(index) with manual type checking and conversion for columns with variable or uncertain types"],"tags":["arguments-accessor","type-conversion","type-mismatch","junit-params"],"backgroundTag":null,"analyzedSha":"f070c699a08b5d8393df9afd147af5c5e90bb21b","analyzedAt":"2026-08-11T20:31:00.530Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}