Initialize a Nested Bean Using Lombok
07:40 02 Mar 2026

I am trying to get Lombok to work on a nested bean class. But every time I get a null pointer exception.

My parent bean class is defined as:

import java.io.Serializable;

import lombok.Builder;
import org.springframework.data.annotation.Id;

import com.fasterxml.jackson.annotation.JsonIgnore;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class StudentData implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -432700976003393778L;

    @Id
    @JsonIgnore
    private String id;

    PersonalInfo personalInfo;
    
    @JsonIgnore
    private String grade;
    
    @JsonIgnore
    private String attendance;
}

and the nested bean class is:

import java.io.Serializable;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class PersonalInfo implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -432700976003393778L;
    
    private String firstName;
    private String lastName;
    private String ssn;
    private String address;
    private String zipCode;
}

Whenever I try to do set a value to nested bean class in my JUnit I get the below error:

Cannot invoke "com.test.student.dao.PersonalInfo.setFirstName(String)" because the return value of "com.test.student.dao.StudentData.getPersonalInfo()" is null

I tried a lot of annotations from Lombok, but nothing worked.

java javabeans lombok