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.

JorgeAmVF
  • 1,660
  • 3
  • 21
  • 32
user123475
  • 1,065
  • 4
  • 17
  • 29
  • 6
    You can't avoid specifying each field without reflection. – shmosel Jan 04 '18 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? – Juan Carlos Mendoza Jan 04 '18 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 Jan 04 '18 at 20:41
  • 2
    Is there something wrong with what is posted at https://stackoverflow.com/a/43180964/7363235 ? It uses Java 8... – D M Jan 04 '18 at 20:42
  • 1
    @DM you would still need to specify them all somehow, unless reflection is used, you simply can't – Eugene Jan 04 '18 at 20:59
  • Possible duplicate of [What is the best way to know if all the variables in a Class are null?](https://stackoverflow.com/questions/12362212/what-is-the-best-way-to-know-if-all-the-variables-in-a-class-are-null) – Didier L Aug 16 '18 at 15:37

3 Answers3

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.

Paul
  • 19,704
  • 14
  • 78
  • 96
Woodrow Barlow
  • 8,477
  • 3
  • 48
  • 86
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).

Mark
  • 2,260
  • 18
  • 27
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?

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