Home » Android » java – Detect which field != null in POJO class?-Exceptionshub

java – Detect which field != null in POJO class?-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

Here I am trying to get values from Firebase database.

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;
    }

}

Some of those getter fields are null, and checking one by one is lengthy coding.

activityRef.child(uid).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot)
    { 
        User usr =dataSnapshot.getValue(User.class); 
        //here i want to check which value!=null 
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});
How to&Answers:

You can use a reflective solution but it will be far slower than if (object.getField() != null) {...}

As I’ve undestand it, there’s only one value that is non-null?

You can achieve it like this:

User class:

class User {
    private String id;
    private String name;

    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;
    }

    // this uses reflection to get the first non-null field
    // you can modify it to return a list of non-null fields
    // in the case I've misunderstood the point
    public Field getFirstNonNullField() {
      for (Field f : getClass().getDeclaredFields()) {
        f.setAccessible(true);
        try {
          if (f.get(this) != null)
            return f;
        } catch (IllegalArgumentException | IllegalAccessException e) {
          // TODO handle this exception as you want
          e.printStackTrace();
        }
      }
      return null;
    }
}

Main method // example of use

public static void main(String[] str) {
    User u = new User();
    u.setName("hello");
    Field f = u.getFirstNonNullField();
    try {
      System.out.print(f.get(u));
    } catch (IllegalArgumentException | IllegalAccessException e) {
      // TODO handle this exception as you want
      e.printStackTrace();
    }
  }

Answer:

I think this will help you to find solution.

activityRef.child(uid).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot)
            { 
                User usr =dataSnapshot.getValue(User.class); 
                for (Field f : User.class.getDeclaredFields()){
                   if(f.get(usr) != null){

                    }
                }
            }

            @Override
            public void onCancelled(@NonNull DatabaseError databaseError) {

            }
        });