0

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?

2 Answers 2

3

You can use the Safe Navigation Operator (?.) to safely ignore null values:

accountWrapper.personDeathYear = person.death?.yearOfDeath;

This returns null the moment a NullPointerException would instead be thrown. This is far easier than adding getters or explicitly checking null values via ternary operators.

1

There are a lot of ways to do it. Here I would probably just add a getter on Person.

public class Person
{
    public Death death;
    public Integer yearOfDeath
    {
        get
        {
            return death == null ? null : death.yearOfDeath;
        }
    }
}
7
  • so its basically like adding if statements at every level, but using getter methods, right? Commented Jan 5, 2021 at 16:53
  • Yeah. And you can one line it. You have to put the if statements somewhere. But you haven't really shown us a clear picture of how you're consuming this data, so it's hard to say what would be the cleanest approach. Commented Jan 5, 2021 at 16:58
  • I am doing JSON.deserialize(response.getBody(), Person.class), if you meant by consume. Commented Jan 5, 2021 at 17:02
  • also why are you putting yearOfDeath under Person class? it is a variable under Death class Commented Jan 5, 2021 at 17:06
  • 3
    P.S. New Safe Navigation Operator: return death?.yearOfDeath; Commented Jan 5, 2021 at 18:15

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.