{"record":{"id":"ee842f4883cd55eb","repo":"chinabugotech/hutool","slug":"the-object-to-build-a-hash-code-for-must-not-be-nu","errorCode":null,"errorMessage":"The object to build a hash code for must not be null","messagePattern":"The object to build a hash code for must not be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hutool-core/src/main/java/cn/hutool/core/builder/HashCodeBuilder.java","lineNumber":337,"sourceCode":"     *            the Object to create a <code>hashCode</code> for\n     * @param testTransients\n     *            whether to include transient fields\n     * @param reflectUpToClass\n     *            the superclass to reflect up to (inclusive), may be <code>null</code>\n     * @param excludeFields\n     *            array of field names to exclude from use in calculation of hash code\n     * @return int hash code\n     * @throws IllegalArgumentException\n     *             if the Object is <code>null</code>\n     * @throws IllegalArgumentException\n     *             if the number is zero or even\n     * @since 2.0\n     */\n    public static <T> int reflectionHashCode(final int initialNonZeroOddNumber, final int multiplierNonZeroOddNumber, final T object,\n            final boolean testTransients, final Class<? super T> reflectUpToClass, final String... excludeFields) {\n\n        if (object == null) {\n            throw new IllegalArgumentException(\"The object to build a hash code for must not be null\");\n        }\n        final HashCodeBuilder builder = new HashCodeBuilder(initialNonZeroOddNumber, multiplierNonZeroOddNumber);\n        Class<?> clazz = object.getClass();\n        reflectionAppend(object, clazz, builder, testTransients, excludeFields);\n        while (clazz.getSuperclass() != null && clazz != reflectUpToClass) {\n            clazz = clazz.getSuperclass();\n            reflectionAppend(object, clazz, builder, testTransients, excludeFields);\n        }\n        return builder.toHashCode();\n    }\n\n    /**\n     * <p>\n     * Uses reflection to build a valid hash code from the fields of {@code object}.\n     * </p>\n     *\n     * <p>\n     * This constructor uses two hard coded choices for the constants needed to build a hash code.","sourceCodeStart":319,"sourceCodeEnd":355,"githubUrl":"https://github.com/chinabugotech/hutool/blob/8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442/hutool-core/src/main/java/cn/hutool/core/builder/HashCodeBuilder.java#L319-L355","documentation":"HashCodeBuilder.reflectionHashCode computes a hash by reflecting over the object's fields; a null object has no fields to read, so it is rejected immediately with IllegalArgumentException. The method also requires non-zero odd seed/multiplier numbers (validated elsewhere). This mirrors Apache Commons Lang's contract.","triggerScenarios":"Calling HashCodeBuilder.reflectionHashCode(...) (any overload delegating here) with a null first-arg object. Common when building equals/hashCode pairs that forget the null branch before delegating.","commonSituations":"equals() implementations that call reflectionHashCode without first null-checking this/other; collections containing nulls that hash via this builder.","solutions":["Null-check before calling: return 0 (or Objects.hashCode) for null.","Prefer Objects.hash / IDE-generated hashCode for performance-critical code; reserve reflectionHashCode for convenience.","If building equals/hashCode, guard `if (obj == null) return false;` then hash."],"exampleFix":"// before\npublic int hashCode() { return HashCodeBuilder.reflectionHashCode(17, 37, this, false); } // NPE risk if this ever null via static call\n// after\npublic static int hashOf(MyType o) { return o == null ? 0 : HashCodeBuilder.reflectionHashCode(17, 37, o, false); }","handlingStrategy":"type-guard","validationCode":"if (object == null) throw new IllegalArgumentException(\"object must not be null\");","typeGuard":"static <T> boolean hashable(T o) { return o != null; }","tryCatchPattern":null,"preventionTips":["Null-check before calling reflectionHashCode.","Prefer Objects.hash / IDE-generated hashCode for hot paths."],"tags":["hashcode","reflection","null-check"],"backgroundTag":null,"analyzedSha":"8870454b2a0c29cc6ffd31dcf5667c8ceb2fc442","analyzedAt":"2026-08-14T04:01:12.892Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}