63

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.

th3an0maly
  • 3,360
  • 8
  • 33
  • 54

15 Answers15

105

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.

Didier L
  • 18,905
  • 10
  • 61
  • 103
  • 1
    Is there any way to get the field values eg: Stream.of(id, name), remove null fields and return List of values of not null fields – Rinsen S Mar 03 '18 at 13:31
  • 1
    @Rinsen you can use `filter()` but that's a different question and it's likely to have already been asked on SO. – Didier L Mar 03 '18 at 13:39
57

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.

arshajii
  • 127,459
  • 24
  • 238
  • 287
  • 1
    Performance wise, is this far behind the `if(variable==null)` method? – th3an0maly Sep 12 '12 at 10:07
  • 2
    Yes, it will likely take a lot longer than `if (var == null)`. – arshajii Sep 12 '12 at 11:00
  • 6
    @Yar, to make this work for private fields, you should do f.setAccessible(true) – Jawa Mar 22 '18 at 18:30
  • 2
    using reflection is quite slow, hide a lot of what is actually done and on a more general thought, it can be a pain to refactor (a field can be marked as unused by your IDE when it is actually via reflection :/ and you will never know it was actually used unless you have good tests or you run your app) Of course this doesn't forbid to use reflection, but if you can use any other option.. go for it – minioim Aug 08 '19 at 13:03
  • 2
    It will never too much to say that you need to add `f.setAccessible(true)` in order to avoid IllegalAccessException on private fields. – Ara Kokeba Nov 21 '19 at 20:03
  • 1
    @AraKokeba you don’t need `setAccessible(true)` when accessing your own `private` fields. The truly problematic part of this code is the use of `getClass()` which may evaluate to a subclass if this class is not `final`. – Holger Feb 09 '23 at 08:04
  • @Holger You're right about the example. I meant accessing private fields outside their class (e.g. a test). – Ara Kokeba Feb 09 '23 at 15:53
37

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.)
Martin Tarjányi
  • 8,863
  • 2
  • 31
  • 49
  • 1
    This will create an extra object `EMPTY` along with each object. Will this affect the performance? – Sp4Rx Mar 05 '19 at 00:34
  • 4
    Empty is a static field here, so it shouldn't be a problem. – Martin Tarjányi Jul 25 '19 at 17:13
  • 1
    @MartinTarjányi but equals() method in java compares instances and not their field values.. you'll need to override equals() and hashCode(). – Tamim Attafi Jan 15 '20 at 06:09
  • 4
    that's why I used the lombok annotation – Martin Tarjányi Jan 15 '20 at 06:10
  • I find myself needing to do something similar to the OP’s question and my thoughts turned first to the equals method. I originally thought to have an isEmpty method that compared to a new instance of the class, but I like the idea of using a static final object more. Nice solution! – Calvin P. Feb 22 '23 at 11:51
21

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

assertThat(myObj).hasNoNullFieldsOrProperties();
b15
  • 2,101
  • 3
  • 28
  • 46
6

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.
        }
    });
}
Igor Oskar
  • 61
  • 1
  • 2
5

"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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
  • 2
    But if I do this, I'm gonna have to edit the method each time I add a field to the class. Besides, a generic method can be put in an abstract class and extended to be used in all other Value Objects – th3an0maly Sep 12 '12 at 10:08
  • 4
    Yes, you will. I'm not sure why you think that's a problem. You _know_ what fields you're adding, you have to create setters and getters for them. Adding a simple one extra line of checking is not a lot of extra work. – paxdiablo Sep 12 '12 at 10:23
  • I think OP means "What is the easiest, most efficient way". If that's the case then this is, in my personal opinion, a terrible answer – birgersp Sep 14 '16 at 12:08
  • 2
    @Birger, if that's what they meant, they should have said that. They've had ample opportunity to change it in the intervening four-or-so years :-) In any case, as others have also suggested, this is a perfectly acceptable method unless your class is so unstable, it's having member variables added or removed massively frequently. – paxdiablo Sep 14 '16 at 12:20
  • 1
    I concurr, the classes should not be changed. OP *did* however write that he'd rather not have to check `(variable == null)` for each variable, and I sympathize with that. It would be neat to have some general way of checking if fields in any given object are null (or not) – birgersp Sep 14 '16 at 15:18
  • 1
    @paxdiablo just as an aside, one argument for a more dynamic approach is when you're converting, say, a DTO to a JPA entity-style object or something. If you don't have a way of dynamically checking for any null properties, it gets harder to write a unit test that will fail when you forget to add a conversion for that new property you just added to the DTO. – ebwb Apr 16 '19 at 20:23
4

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();
    
          return  isEmpty > 0

    }
orly.sharon
  • 149
  • 1
  • 9
3

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());
    }
}
Domenic D.
  • 5,276
  • 4
  • 30
  • 41
2
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...
                }
}
  • 1
    Instead of comparing `field[j].getGenericType().toString()` with `"class java.lang.String"`, you can use a straight-forward check of the actual type, i.e. `if(field[j].getType() == String.class)` That’s simpler and way more efficient. – Holger Feb 09 '23 at 08:13
2

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.

lrkwz
  • 6,105
  • 3
  • 36
  • 59
  • 1
    This will always be `false`, as the array returned by `getDeclaredMethods()` never contains `null`. You’d have to actually invoke those methods if you want to check whether they return `null`, but mind that not every method is a getter. – Holger Feb 09 '23 at 08:07
1

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.

HowToTellAChild
  • 623
  • 1
  • 9
  • 21
  • 1
    “Based on Irkwz's answer” and similarly pointless. You are not checking the values of the fields but only if the array of `Field` instances returned by `getDeclaredFields()` contains `null`, which is always `false`. You’d have to actually `get()` the field’s value before checking. And when accessing `private` fields, you should use `SomeClass.class` instead of `getClass()`, because `getClass()` may return a subclass. – Holger Feb 09 '23 at 08:11
0

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
 }
}
sarthakgupta072
  • 451
  • 6
  • 13
0

Easiest way is to convert the class to a map and get its keys and with stream check if any or all key's values are null or not, you can take input from user as well whether they want to check for specific set of keys only!

Below is the code to check whether any of the key's value has null, you can change stream config to all match or any match as per your requirement

Just replace isNullOrEmpty method i have used with proper null or empty check condition for that particular collection

    public boolean checkIfAnyFieldIsNull(Object instance, Set<String> fields){

        try {
            Map<String, Object> instanceMap = new Gson().fromJson(new GsonBuilder().serializeNulls().create().toJson(instance), Map.class);

            if(!isNullorEmpty(instanceMap)) {
                fields = isNullorEmpty(fields) ? instanceMap.keySet() : fields;
                return fields.stream().anyMatch(curField -> isNull(instanceMap.get(curField)));
            }else{
                return false;
            }
        }catch (Exception e){
            return false;
        }
    }
}
kmj
  • 41
  • 2
  • 7
0

Try this method once, its works for me!!

private fun checkIfAnyDataIsNull(model: YourModelCass): Boolean {
    return Stream.of<Any>(
        model.date,
        model.merchantName,
        model.payment,
    ).allMatch(Objects::isNull)
}
Mehul Boghra
  • 202
  • 3
  • 11
0

You can use the simple solution:

if(user.equals(new User()){
//your processing goes here
}
Abderrahmen
  • 440
  • 3
  • 6