Amrish Kumar
Amrish Kumar

Reputation: 318

Best way to track null value set to a field in Java POJO class

I have a Record class as follows

class Record {
  String name;
  Object value;
  public void setValue(Object value) { this.value = value; }
}

If no value was set to the value field then it would be null. It is also possible that null was explicitly set using the setter i.e new Record().setValue(null).

Since explicitly setting the value as null has meaning in my application, I want to track whether the null value was explicitly set by user or is just an implicit null. What is the best way and the proper way to do it?

The solutions I came up with were,

If this was JavaScript, it has undefined built in the language itself. What is the best way to do this Java?

Upvotes: 0

Views: 5287

Answers (2)

Stephen C
Stephen C

Reputation: 719576

What is the best way and the proper way to do it?

There is no single "best" or "proper" way to do this, though it is probably a bad idea to try to distinguish different causes or kinds of null.

Here are some of the alternatives.

  1. Avoid the problem by making null an illegal value; e.g.

     public void setValue(Object value) { 
         if (value == null) {
             throw new NullPointerException("Value cannot be null");
         } else {
             this.value = value; 
         }
     } 
    
  2. Use Optional<Object>.

  3. Use the Null Object Pattern.

  4. Use zero length arrays and (immutable) empty collections rather than null arrays and collections. (The "null means empty" optimization is a bad idea. It makes your code more complicated and more NPE prone and the space saving is minuscule.)

  5. Avoid the problem by insisting that the field is initialized to a non-null value. This could be done using the Builder Pattern, or using a constructor that insists that the supplied value for value not null.

  6. Use a special value to mean "this field has not been initialized"; e.g.

     private static final Object UNINITIALIZED = new Object();
     private Object value = UNINITIALIZED;
    
     public void getValue() {
         if (value == UNINITIALIZED) {
             throw new UninitializedFieldException("Value is uninitialized");
         } else {
             return value;
         }
     }
    

    Notes:

    • This is a variation of the Null Object Pattern.
    • You don't need a special class to do this.
    • It is best to keep the instance (or class) hidden. Therefore the value field needs to be private and the getter needs to throw an exception if the field is "uninitialized.
  7. Use a boolean to denote that the field has not been initialized.

Upvotes: 2

Katy
Katy

Reputation: 1157

I think considering null value as user entry isn't the best solution since later on, you can't differentiate a null coming from the user from a null field that wasn't set programmatically. (i.e. If your client needs to update the record, you will not be able to distinguish whether he sets the value to null or he doesn't want to update it, hence you can overwrite a previous value).

So, if I were in you place, I would replace the null with some meaningful object value.

Upvotes: 0

Related Questions