Best way to check NULL for objects in Java

I have a solution to check for NULL values retrieved from an object. However, I believe that there may be a better approach than I am here. So please suggest the best ways with a code snippet :)

I will pass my xml content to the unmarshalling method and pass the unmarshalledValues ​​to a zero checking method (e.g. ValidateInputFiled)

Contents unmarshalledValues = unmarshalingContent( xml ); inputCheck = ValidateInputField( unmarshalledValues ); 

I have a POJO for my XML elements as follows,

  @XmlRootElement( name = "contents" ) public class Contents { @XmlElement String A; @XmlElement String B; @XmlElement String C; @XmlAttribute String D; public String getA() { return A; } public String getB() { return B; } public String getC() { return C; } public String getD() { return D; } } 

I defined ValidateInputFiled as below

 public Boolean ValidateInputField( Contents unmarshalledValues ) { int checker = 0; Boolean listCheck = false; // Extracting unmarshalled values from xml String A= unmarshalledValues.getA(); String B= unmarshalledValues.getB(); String C = unmarshalledValues.getC(); String D= unmarshalledValues.getD(); if ( A== null || A.isEmpty() ) { checker++; } if ( B== null || B.isEmpty() ) { checker++; } if ( C== null || C.isEmpty() ) { checker++; } if ( D== null || D.isEmpty() ) { checker++; } if ( checker == 0 ) { listCheck = true; } return listCheck; } 

Here I am looking to avoid NULL checking for each row value (e.g. A, B, C, D) instead, can I just do a null content check or for unmarshalledValues ​​using a collection or list?

+2
source share
5 answers

Is this what you are looking for?

 public Boolean ValidateInputField(Contents unmarshalledValues) { // Extracting unmarshalled values from xml String A = unmarshalledValues.getA(); String B = unmarshalledValues.getB(); String C = unmarshalledValues.getC(); String D = unmarshalledValues.getD(); return checkNull(A, B, C, D); } private static boolean checkNull(String... strings) { for (String string : strings) { if (string == null || string.isEmpty()) { return false; } } return true; } 
+4
source
 public static boolean isNullOrEmpty(String a) { return a == null || a.isEmpty(); } 

Call it for each value. You might consider adding them all to the list, and then iterating through them, increasing the validation, if any! IsNullOrEmpty to save code bloat if you have many fields.

PS: make your fields private to preserve encapsulation.

pps: no need to worry about a separate boolean just return checker == 0; to keep the code tidy.

+6
source

I am using the StringUtils apache library for this type of thing. It has a check that includes empty or empty spaces, as well as other combinations depending on how you handle empty spaces. I really liked code like Jeff, but I like the other methods that they include.

You can also avoid alltogether null values ​​by encoding your getters to return "" if value == null. Then you would not need to check each field for zero.

0
source

commons-lang has a Validate class that you could use:

 Validate.notNull( unmarshalledValues.getA() ); 
0
source

A non-reflective solution for Java 8 without using if's series would have to pass all the fields and check for absence:

 return Stream.of(id, name).allMatch(Objects::isNull); 

This remains fairly easy to maintain, avoiding reflection. This will return true for null attributes.

0
source

Source: https://habr.com/ru/post/959411/


All Articles