I have a json response class like this:
public with sharing class Response {
public Person person;
public class Person {
public PersonData personData = new PersonData ();
public Death death = new Death();
}
.....
public class Death {
public String yearOfDeath;
public String dayOfDeath;
public String monthOfDeath;
}
....
}
I can get a response such as:
{
"Person": {
PersonData: {
"Name":"x"
"Surname":"x",
"DateOfBirth":"12-12-1980",
}
}
Note that there is no Death in the response. The callout class captures the response, Response personResp = JSON.deserialize(response.getBody(), Response.class)) and sends personResp to the lwc controller class, where I have an AccountWrapper class, which has variables for which the values of personResp will be assigned like this:
AccountWrapper accountWrapper = new AccountWrapper();
accountWrapper.personDeathYear = person.death.yearOfDeath;
This wrapper class is sent to front end for displaying the fields. The problem is if I do:
accountWrapper.personDeathYear = person.death.yearOfDeath;
I get Attempt to de-reference a null object. Now, I can put null check at 2nd level i.e., for death, but I have others that go like this:
accountWrapper.streetName = person.addressData.address.addressEnglish.streetName; //5 levels
Is there a better way than putting null check at every level for each accountWrapper variable?