'What is the best way to Null check input fields in the request object in Java?

I have a Code Like this:

Insurance Dto:

 private String accidentNo;
 private String birthDate;
 private String mobileNo;
 private String ssn;

 public InsuranceDto(String accidentNo, String birthDate, String mobileNo, String ssn) {
    this.accidentNo = accidentNo;
    this.birthDate = birthDate;
    this.mobileNo = mobileNo;
    this.ssn = ssn;
 }

 public InsuranceDto() {}

 public String getAccidentNo() {
    return accidentNo;
 }

 public void setAccidentNo(String accidentNo) {
    this.accidentNo = accidentNo;
 }

 public String getBirthDate() {
    return birthDate;
 }

 public void setBirthDate(String birthDate) {
    this.birthDate = birthDate;
 }

 public String getMobileNo() {
    return mobileNo;
 }

 public void setMobileNo(String mobileNo) {
    this.mobileNo = mobileNo;
 }

 public String getSsn() {
    return ssn;
 }

 public void setSsn(String ssn) {
    this.ssn = ssn;
 }

Main Method:

 InsuranceDto insuranceDto = new InsuranceDto();

 if (insuranceDto.getAccidentNo() == null)
     throw new RuntimeException("Enter AccidentNo.");
 if (insuranceDto.getBirthDate() == null)
     throw new RuntimeException("Enter BirthDate.");
 if (insuranceDto.getMobileNo() == null)
     throw new RuntimeException("Enter MobileNo");
 if (insuranceDto.getSsn() == null)
     throw new RuntimeException("Enter SSN");

So the Code Works but its Kind of Garbage and if I Add 3 Other Fields to InsuranceDTO, I Have to Edit the Entire Code to Validate them. Imagen if the Validation Policy Gets more Complicated and you Need to Validate two and/or more Fields in one If statement At Once.

So How Can I avoid this? Is There a Design Pattern or Something else to Make it Better?



Solution 1:[1]

Since you are trying to reduce the amount of code, I can suggest you to Use Lombok @Data annotation to get intrinsic getters and setters as well, this way you can avoid the whole getter setter methods which can reduce a lot of code. The way your InsuranceDto looks after you implement lombok is:

@Data
@NoArgsConstructor
@AllArgsConstructor
@Build
public class InsuranceDto {
    @NotNull(message ="Accident Number must not be empty null")
    private String accidentNo;
    @NotNull(message ="Sample field 2 must not be empty null")
    private String samplefield2;
}

Lombok repository in Maven:

<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
    <version>1.18.22</version>
    <scope>provided</scope>
</dependency>

And to your question, In InsuranceDto, you can attach the @Data once you have the lombok, for example above the field accidentNo in the InsuranceDto, you can have

@NotNull(message ="Accident Number must not be empty null")
private String accidentNo;

This way you can completely avoid the null checks within your service class because it will throw a runtime exception when such a null values comes in even before hitting your service. The validation is automatic.

Edit: If you're only trying to avoid null checks you can use Apache commons.

 org.apache.commons.lang3.Validate

Usage: If the accidentNo is null, an error is thrown with the string you give as a second argument.

Validate.notNull(accidentNo, "accidentNo cannot be null")

Sources

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

Source: Stack Overflow

Solution Source
Solution 1