Consider the POJO as below:
class Person {
private String name;
private Address address;
private UserProperties properties;
//getters, stetters, etc
}
class Adddress {
private String state;
private Street street;
private String country;
}
class Street {
private String line1;
private String line2;
}
class PersonAttributes {
private String nationality;
private String age;
}
So the scenario is this:
- I do not have control over the POJO, it is a predefined structure.
- Any of the fields can be null.
- It is just a sample, the actual POJO is much more complex and nested.
When I query the field I have to add a null check to prevent null pointer exception.
For example:
if (person.getAddress() != null) {
//do an operation on address
if (person.getAddress().getStreet() != null) {
//do operation on street
}
}
Similarly for attributes and other nested fields.
Is there any alternative to the above approach that will reduce the complexity and avoid all these null checks?
I am fetching all the fields form a REST API and using Jackson to assign it to a POJO.
Is this approach correct for a highly nested JSON structure or there are other options which I should be using?