What is the best way to know if all the variables in a Class are null?

Java

Java Problem Overview


This would mean that the class was initialized, but the variables were not set.

A sample Class:

public class User {

    String id = null;
    String name = null;

    public String getId() {
	    return id;
    }
    public void setId(String id) {
	    this.id = id;
    }

    public String getName() {
	    return name;
    }
    public void setName(String name) {
	    this.name = name;
    }
}

The actual class is huge that I prefer not to check if(xyz == null) for each of the variables.

Java Solutions


Solution 1 - Java

Another non-reflective solution for Java 8, in the line of paxdiabo's answer but 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.

Solution 2 - Java

Try something like this:

public boolean checkNull() throws IllegalAccessException {
    for (Field f : getClass().getDeclaredFields())
    	if (f.get(this) != null)
    	    return false;
    return true;    		
}

Although it would probably be better to check each variable if at all feasible.

Solution 3 - Java

This can be done fairly easily using a Lombok generated equals and a static EMPTY object:

import lombok.Data;

public class EmptyCheck {
    public static void main(String[] args) {
        User user1 = new User();

        User user2 = new User();
        user2.setName("name");

        System.out.println(user1.isEmpty()); // prints true
        System.out.println(user2.isEmpty()); // prints false
    }

    @Data
    public static class User {
        private static final User EMPTY = new User();

        private String id;
        private String name;
        private int age;

        public boolean isEmpty() {
            return this.equals(EMPTY);
        }
    }
}

Prerequisites:

  • Default constructor should not be implemented with custom behavior as that is used to create the EMPTY object
  • All fields of the class should have an implemented equals (built-in Java types are usually not a problem, in case of custom types you can use Lombok)

Advantages:

  • No reflection involved
  • As new fields added to the class, this does not require any maintenance as due to Lombok they will be automatically checked in the equals implementation
  • Unlike some other answers this works not just for null checks but also for primitive types which have a non-null default value (e.g. if field is int it checks for 0, in case of boolean for false, etc.)

Solution 4 - Java

If you want this for unit testing I just use the hasNoNullFieldsOrProperties() method from assertj

assertThat(myObj).hasNoNullFieldsOrProperties();

Solution 5 - Java

How about streams?

public boolean checkFieldsIsNull(Object instance, List<String> fieldNames) {
    
    return fieldNames.stream().allMatch(field -> {
        try {
            return Objects.isNull(instance.getClass().getDeclaredField(field).get(instance));
        } catch (IllegalAccessException | NoSuchFieldException e) {
            return true;//You can throw RuntimeException if need.
        }
    });
}

Solution 6 - Java

"Best" is such a subjective term :-)

I would just use the method of checking each individual variable. If your class already has a lot of these, the increase in size is not going to be that much if you do something like:

public Boolean anyUnset() {
    if (  id == null) return true;
    if (name == null) return true;
    return false;
}

Provided you keep everything in the same order, code changes (and automated checking with a script if you're paranoid) will be relatively painless.

Alternatively (assuming they're all strings), you could basically put these values into a map of some sort (eg, HashMap) and just keep a list of the key names for that list. That way, you could iterate through the list of keys, checking that the values are set correctly.

Solution 7 - Java

The best way in my opinion is Reflection as others have recommended. Here's a sample that evaluates each local field for null. If it finds one that is not null, method will return false.

public class User {

	String id = null;
	String name = null;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public boolean isNull() {
		Field fields[] = this.getClass().getDeclaredFields();
		for (Field f : fields) {
			try {
				Object value = f.get(this);
				if (value != null) {
					return false;
				}
			}
			catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			

		}
		return true;

	}

	public static void main(String args[]) {
		System.out.println(new User().isNull());
	}
}

Solution 8 - Java

I think this is a solution that solves your problem easily: (return true if any of the parameters is not null)

  public boolean isUserEmpty(){ 
boolean isEmpty;
isEmpty =  isEmpty = Stream.of(id,
            name)
        .anyMatch(userParameter -> userParameter != null);

return isEmpty;}

Another solution to the same task is:(you can change it to if(isEmpty==0) checks if all the parameters are null.

public boolean isUserEmpty(){  
       long isEmpty;
            isEmpty = Stream.of(id,
                    name)
                    .filter(userParameter -> userParameter != null).count();
    
            if (isEmpty > 0) {
                return true;
            } else {
                return false;
            }
    }

Solution 9 - Java

Field[] field = model.getClass().getDeclaredFields();     

for(int j=0 ; j<field.length ; j++){    
            String name = field[j].getName();                
            name = name.substring(0,1).toUpperCase()+name.substring(1); 
            String type = field[j].getGenericType().toString();    
            if(type.equals("class java.lang.String")){   
                Method m = model.getClass().getMethod("get"+name);
                String value = (String) m.invoke(model);    
                if(value == null){
                   ... something to do...
                }
}

Solution 10 - Java

Best for me is

Stream.of(getClass().getDeclaredMethods()).allMatch(Objects::isNull);

It can be used in a custom annotation + annotation processor to automagically define a boolean isNull() method on the annotated classes.

Solution 11 - Java

If you want to do the opposite i.e check if some/all members of class are non-non, the check this answer.

In order to make sure that certain members of the class are always non-null, we can use lombok @NonNull annotation on the individual fields of the class.

import lombok.Data;
import lombok.NonNull;

@Data
public class DataClass {
  @NonNull
  private String data1;
  private int data2;
  @NonNull
  private String data3;
  @NonNull
  private String data4;
  @NonNull
  private String data5;
  private String data6;

 DataClass(String data1,...) {
    // constructor
 }
}

Solution 12 - Java

Based on Irkwz's answer, but a different approach:

public class SomeClass{

private String field1;
private String field2;
private ComplexField field3;
private String field4;
private Integer field15;

public boolean isNullAllFields() {
	return Stream.of(this.getClass().getDeclaredFields()).anyMatch(element -> (element != null));
}

}

And the end of the day u invoke isNullAllFields method to figure out wheter the object fields are empty.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionth3an0malyView Question on Stackoverflow
Solution 1 - JavaDidier LView Answer on Stackoverflow
Solution 2 - JavaarshajiiView Answer on Stackoverflow
Solution 3 - JavaMartin TarjányiView Answer on Stackoverflow
Solution 4 - Javab15View Answer on Stackoverflow
Solution 5 - JavaIgor OskarView Answer on Stackoverflow
Solution 6 - JavapaxdiabloView Answer on Stackoverflow
Solution 7 - JavaDomenic D.View Answer on Stackoverflow
Solution 8 - Javaorly.sharonView Answer on Stackoverflow
Solution 9 - JavarookiepupilView Answer on Stackoverflow
Solution 10 - JavalrkwzView Answer on Stackoverflow
Solution 11 - Javasarthakgupta072View Answer on Stackoverflow
Solution 12 - JavaHowToTellAChildView Answer on Stackoverflow