2

I have a solution to check NULL values extracted from object, However i feel there might be best approach than i am doing here. So please suggest me the best ways with code snippet :)

I will be passing my xml Content to unmarshalling method & then pass the unmarshalledValues to null check method (i.e ValidateInputFiled )

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

I have a POJO for my XML elements as mentioned below,

 @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 have defined ValidateInputFiled as mentioned 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 check for each String Values ( i.e A, B, C, D ) instead can i just do null check for Contents or for unmarshalledValues using collection or list ?

1
  • You can also avoid nulls alltogether by coding your getters to return "" if a value == null. Then you would not have to check each field for null. Commented Jun 8, 2012 at 15:33

5 Answers 5

6
public static boolean isNullOrEmpty(String a) {
    return a == null || a.isEmpty();
}

Call that for each value. You may want to think about adding them all to a list and then iterating through them, incrementing checker if they're !isNullOrEmpty to save code bloat if you have lots for fields.

PS: Make your fields private to preserve encapsulation.

pps: don't bother with a seperate boolean just return checker == 0; to keep the code neat.

Sign up to request clarification or add additional context in comments.

1 Comment

Rather than counting, OP can just return false; in the body of each test, and return true; at the end of the method (which will be reached only if all tests pass).
4

Is that 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;
}

1 Comment

This would be even better if you changed checkNull to return a boolean value (e.g., noNulls. (It would return false inside the if and true at the end of the method.) Then ValidateInputField could simply return the result of calling noNulls.
0

I use the apache commons StringUtils library for this type of thing. It has a check that includes null or empty spaces, plus other combinations depending on how you treat empty spaces. Pretty much code like Jeff here gave you, but i like having other methods they include.

You can also avoid nulls alltogether by coding your getters to return "" if a value == null. Then you would not have to check each field for null.

Comments

0

commons-lang has a Validate class you could use:

Validate.notNull( unmarshalledValues.getA() );

Comments

0

Non-reflective solution for Java 8, without using a series of if's, would be to stream all fields and check for nullness:

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

This remains quite easy to maintain while avoiding the reflection hammer. This will return true for null attributes.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.