{"record":{"id":"59df3cf944ab3ed9","repo":"jwtk/jjwt","slug":"value-cannot-be-represented-as-a-java-lang-integer","errorCode":null,"errorMessage":"Value cannot be represented as a java.lang.Integer.","messagePattern":"Value cannot be represented as a java\\.lang\\.Integer\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"impl/src/main/java/io/jsonwebtoken/impl/lang/PositiveIntegerConverter.java","lineNumber":46,"sourceCode":"        return integer;\n    }\n\n    @Override\n    public Integer applyFrom(Object o) {\n        Assert.notNull(o, \"Argument cannot be null.\");\n        int i;\n        if (o instanceof Byte || o instanceof Short || o instanceof Integer || o instanceof AtomicInteger) {\n            i = ((Number) o).intValue();\n        } else {  // could be Long, AtomicLong, Float, Decimal, BigInteger, BigDecimal, String, etc., all of which\n            // may not be accurately converted into an Integer, either due to overflow or fractional values.  The\n            // easiest way to account for all of them is to parse the string value as an int instead of testing all\n            // the types:\n            String sval = String.valueOf(o);\n            try {\n                i = Integer.parseInt(sval);\n            } catch (NumberFormatException e) {\n                String msg = \"Value cannot be represented as a java.lang.Integer.\";\n                throw new IllegalArgumentException(msg, e);\n            }\n        }\n        if (i <= 0) {\n            String msg = \"Value must be a positive integer.\";\n            throw new IllegalArgumentException(msg);\n        }\n        return i;\n    }\n}\n","sourceCodeStart":28,"sourceCodeEnd":56,"githubUrl":"https://github.com/jwtk/jjwt/blob/fb71496164c71442d08adec4571d9616ed5e1b8d/impl/src/main/java/io/jsonwebtoken/impl/lang/PositiveIntegerConverter.java#L28-L56","documentation":"PositiveIntegerConverter.applyFrom converts an input object to an Integer by first attempting native conversion and falling back to Integer.parseInt(String.valueOf(o)). If that parse fails with NumberFormatException, the value cannot be represented as an Integer and this IllegalArgumentException is thrown (with the NumberFormatException as cause).","triggerScenarios":"Passing a non-integer string like \"abc\", \"1.5\", or an arbitrary object whose toString() is not a valid integer to a converter/claim that expects a positive integer value.","commonSituations":"Setting claims such as 'typ' header params or payload fields expecting integer counts with decimal or textual values; JSON values parsed as Double (1.0) being converted; copying claims from loosely typed maps.","solutions":["Supply a genuine Integer/int value instead of a string or floating-point number.","If the value is a numeric string, ensure it has no decimals, signs beyond '-', or whitespace before conversion.","Pre-parse with Integer.parseInt yourself inside try/catch to give a clearer error."],"exampleFix":"// before\njwtBuilder.claim(\"rate\", \"1.5\");\n// after\njwtBuilder.claim(\"rate\", 2);","handlingStrategy":"type-guard","validationCode":"Integer asPositiveInt(Object o) {\n    if (o instanceof Integer i) return i;\n    if (o instanceof Number n && n.intValue() == n.doubleValue()) return n.intValue();\n    return null; // not representable as an Integer\n}","typeGuard":"boolean isIntegralValue(Object o) {\n    return o instanceof Integer\n        || (o instanceof Number n && n.doubleValue() == n.intValue());\n}","tryCatchPattern":"try {\n    converter.applyFrom(value);\n} catch (IllegalArgumentException e) {\n    if (e.getCause() instanceof NumberFormatException) {\n        // value not parseable as an integer\n    }\n    throw e;\n}","preventionTips":["Pass native int/Integer values rather than strings","Reject floating-point JSON numbers where integers are expected","Trim and sanitize numeric strings before conversion"],"tags":["type-conversion","integer","illegal-argument"],"backgroundTag":"type-mismatch","analyzedSha":"fb71496164c71442d08adec4571d9616ed5e1b8d","analyzedAt":"2026-09-09T00:33:09.982Z","contentChangedAt":"2026-09-09T00:33:09.982Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}