Best way to check NULL for objects in Java

2019-05-21 01:14发布

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 ?

标签: java list null
5条回答
不美不萌又怎样
2楼-- · 2019-05-21 01:31

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.

查看更多
一纸荒年 Trace。
3楼-- · 2019-05-21 01:39

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;
}
查看更多
虎瘦雄心在
4楼-- · 2019-05-21 01:46

commons-lang has a Validate class you could use:

Validate.notNull( unmarshalledValues.getA() );
查看更多
不美不萌又怎样
5楼-- · 2019-05-21 01:50
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.

查看更多
甜甜的少女心
6楼-- · 2019-05-21 01:50

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.

查看更多
登录 后发表回答