'How to get value from nested object in Java?

@Getter
@Setter
public class Person {

private String name;
private String age;
private Set<Children> children;

}

@Getter
@Setter
public class Children {

    private String childrenName;
    private String childrenAge;
}

How to get the childrenName and childrenAge from the Person object?

for example I have another test class:

public class Test {
    
    Children p = new Children();
    p.setChildrenName("Nick"); //prints "Nick"

    Person p = new Person();
    Set<String> childrenNames = p.getChildren().stream()
    .map(Children::getChildrenName)
    .collect(Collectors.toSet()); `//gets NullPointerException`

}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source