3

I want to check if all the fields of an object is null or not using Java 8. Tried different approach as mentioned in here but I want it to be done using Java 8 feature.

For example:

class person{
   String name;
   Long id;
   //getter & setter
}
Person person = new Person();
List<Person> personList = new ArrayList<>();
personList.add(person);

I want to add person to list only when id and name is not NULL. Since, Person has more than 10 fields, I dont want to null check each field and add to list. Here, I am setting those fields from ResultSet from DB operation.

6
  • 6
    You can't avoid specifying each field without reflection.
    – shmosel
    Commented Jan 4, 2018 at 20:37
  • 3
    "I want to add person to list only when id and name is not NULL", then why you want to null check all the fields? Commented Jan 4, 2018 at 20:39
  • this is just example that I am giving. I want to add to list of any of the fields of those 10+ fields is not NULL
    – user123475
    Commented Jan 4, 2018 at 20:41
  • 2
    Is there something wrong with what is posted at stackoverflow.com/a/43180964/7363235 ? It uses Java 8...
    – D M
    Commented Jan 4, 2018 at 20:42
  • 1
    @DM you would still need to specify them all somehow, unless reflection is used, you simply can't
    – Eugene
    Commented Jan 4, 2018 at 20:59

3 Answers 3

10

Assuming you know all of the fields at compile-time, the best solution is to check each one explicitly. Using reflection for this task would add unnecessary run-time overhead. However, to save yourself a little effort, you can write a function on the class itself to handle this check.

class Person {
    String name;
    Long id;

    // constructor, getters, and setters
    
    public boolean isEmpty() {
        return (this.name == null && this.id == null);
    }
}

Then your body code might look like this:

Person person = new Person();
if (!person.isEmpty()) {
    personList.add(person);
}

If you don't know all of the fields until run-time, you'll need to use reflection as described by this stackoverflow answer.

1

If you're willing to use an external package you could use the Lombok @NonNull annotation on either the setter of the method or on the constructor of the class. That will generate checking code that will result in a NullPointerException at the time that someone attempts to set the value to null. Essentially, Lombok generates the boiler plate checking code. You just add an annotation (or 10).

Having the exception occur at the time of creating the object is often far preferable to encountering it later (i.e you can catch it and handle it appropriately).

0

Reflection is neat however it comes at a significant cost.

The best approach here would be to add an interface for the object forcing the implementation of a null check method. Inside of the implemented null check method you would then add your logic to see if the item properties are null or not.

If you have your heart set on reflection, which you shouldn't unless you don't know the fields you are receiving, then this question has some answers over here What is the best way to know if all the variables in a Class are null?

1
  • "it comes at a significant cost"... its 2022, even 2018, reflection isn't that expensive unless you're talking about very constrained environments
    – visc
    Commented Sep 12, 2022 at 15:06

Not the answer you're looking for? Browse other questions tagged or ask your own question.