Backend Development 9 min read

Resolving NullPointerException in BeanCopier Caused by Lombok @Accessors(chain=true)

This article analyzes a NullPointerException triggered by Lombok's @Accessors(chain=true) when using BeanCopier, explains how the altered setter return type prevents JDK Introspector from recognizing write methods, and provides two practical solutions to fix the issue.

JD Tech
JD Tech
JD Tech
Resolving NullPointerException in BeanCopier Caused by Lombok @Accessors(chain=true)

Problem description: MCube loads templates and during view rendering a NullPointerException occurs, with a stack trace showing cglib BeanCopier failures.

Analysis: The error originates from using Lombok @Accessors(chain=true), which changes setter methods to return the object itself instead of void. JDK Introspector only treats void‑returning set methods as write methods, so BeanCopier cannot locate a writeMethod for the property, leading to a NullPointerException in ReflectUtils.getMethodInfo .

Relevant code snippets illustrate the stack trace, the Lombok‑generated setter, and the Introspector source that filters setters based on return type.

java.lang.NullPointerException: null
  at net.sf.cglib.core.ReflectUtils.getMethodInfo(ReflectUtils.java:424) ~[cglib-3.1.jar:?]
  at net.sf.cglib.beans.BeanCopier$Generator.generateClass(BeanCopier.java:133) ~[cglib-3.1.jar:?]
  ... (stack trace omitted for brevity)
@Accessors(chain = true)
public class YourClass {
    private int a;

    @Setter
    public YourClass setA(int a) {
        this.a = a;
        return this;
    }
}

The article also examines the copyPropertiesOfList and copyProperties utilities that rely on BeanCopier, confirming that the target class lacks a proper writeMethod due to the Lombok annotation.

Solution 1: Remove the @Accessors(chain = true) annotation and use conventional void setters.

Solution 2: Keep the annotation but replace BeanCopier with a safer mapper such as MapStruct, which generates correct getters/setters at compile time and works well with Lombok.

Conclusion: Any component that depends on JDK Introspector to discover setter methods will fail for classes using Lombok's chainable setters; developers should be aware of this limitation and choose appropriate copying utilities.

JavanullpointerexceptionLombokBeanCopierIntrospectorObjectCopy
JD Tech
Written by

JD Tech

Official JD technology sharing platform. All the cutting‑edge JD tech, innovative insights, and open‑source solutions you’re looking for, all in one place.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.