简体   繁体   中英

Is there a way to check if a class is empty in java?

Sorry if the title is a bit confusing, I didn't really know how to word what I wanted to ask.

Basically, I am making an api call to a database that returns data as such:

[{"profiles":{" testexample ":{"addresses":[{"city":" ","street1":" ","street2":"apt 320"}],"addressType":"HOME","city":" ","dateOfBirth":" ","emailAddress1":" ","emailAddress2":" ","emailAddresses":[{"email":" ","preferred":1,"type":"BUSINESS"},{"email":" ","preferred":0,"type":"PERSONAL"}],"firstName":" ","lastName":" ","phoneNumber":" ","phoneNumbers":[],"phoneType":"HOME","postalCode":" ","preferred":1,"street1":" ","street2":" "}]

The code I have below works fine when the database returns a non-empty profiles {}. I have the following Java classes that looks like the following:

public class Account {

    @JsonProperty("profiles")
    private Profiles profiles;

    @JsonProperty("profiles")
    public Profiles getProfiles() {
        return profiles;
    }

    @JsonProperty("testexample")
    public void setProfiles(Profiles profiles) {
        this.profiles = profiles;
    }

}
public class Profiles {

    @JsonProperty("testexample")
    private Profile testExample;

    @JsonProperty("testexample")
    public Profile getTestExample() {
        return testExample;
    }

    @JsonProperty("testexample")
    public void setTestExample(Profile testExample) {
        this.testExample = testExample;
    }

}
public class Profile {


    @JsonProperty("dateOfBirth")
    private String dateOfBirth;

    @JsonProperty("dateOfBirth")
    public String getDateOfBirth() {
        return dateOfBirth;
    }
    @JsonProperty("dateOfBirth")
    public void setDateOfBirth(String dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

}

So what I want to do when I get the data is check whether the getProfiles() returns empty, so I don't make the calls to anything within that object.

Please note, for the sake of simplicity I omitted other parts of the classes to focus primarily on what I wanted

This is what I have so far, and it works when the profiles {} is not empty

Account response = access.lookup(id, type); //This is to grab the response from the database, which is working.

response.getProfiles(); //This is the part that works when it has a profiles {} not empty, but fails on empty.

So what happens is that I don't get an error for response.getProfiles(), but if I tried to do response.getProfiles().getDateOfBirth(), it won't work because it will give a null pointer exception since the dateOfBirth isn't there.

I want to avoid calling anything within response.getProfiles() by skipping it if it's empty.

You need some basic null checking. The most basic way is to assign a new variable and check.

Profiles profiles = account.getProfiles();
if(profiles != null) {
   //dosomething with profiles.getDateOfBirth()
}

The more "modern" functional Java way would be to use the Optional class.

String dateOfBirth = Optional.ofNullable(account.getProfiles())
    .map(profile -> profile.getDateOfBirth)
    .orElse(null);

(A note about your example: In the Account class, you have this.

@JsonProperty("testexample")
public void setProfiles(Profiles profiles) {
    this.profiles = profiles;
}

Which appears to be an incorrect @JsonProperty annotation and might be causing some problems. That said, it is not necessary to annotate the getters and setters. The one annotation on the field is sufficient.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM