2 Replies Latest reply: Sep 27, 2013 10:56 AM by 3a132ce1-ddfc-47dd-ac30-3e66c70c6b34 RSS

    Properties: Handling of NULL values

    3a132ce1-ddfc-47dd-ac30-3e66c70c6b34

      I wanted to ask how you deal with NULL values concerning JavaFX properties.

      Wrapper properties for primitive data types like SimpleIntegerProperty or SimpleBooleanProperty don´t accept NULL values.

       

      Example:

      1. IntegerProperty integerProperty = new SimpleIntegerProperty();
      2. integerProperty.setValue(null);
      3. System.out.println(integerProperty.getValue());

       

      Output is 0 (default value for "int").

        • 1. Re: Properties: Handling of NULL values
          James_D

          I think of the primitive property wrappers (IntegerProperty, BooleanProperty and friends) as wrappers for primitive types. Arguably they should not really inherit from WritableValue as that allows the call to setValue with a reference type, but I think the API bloat would be worse than implementing setValue with a slightly incorrect signature. I would simply avoid setting a primitive type property wrapper to null: if you restrict yourself to using set(..) instead of setValue(...) you are forced to pass in the primitive type: i.e. you can't pass in null.

           

          If you want a property of type Integer where null values are handled in an intuitive way, use ObjectProperty<Integer> instead.

          It appears from the test below that calling setValue(null) on IntegerProperty sets the value to the default. (Though I can't see that behavior documented anywhere.)

           

          import javafx.beans.property.IntegerProperty;
          import javafx.beans.property.ObjectProperty;
          import javafx.beans.property.SimpleIntegerProperty;
          import javafx.beans.property.SimpleObjectProperty;
          import javafx.beans.value.ChangeListener;
          import javafx.beans.value.ObservableValue;
          
          
          public class PrimitivePropertyWrapperTest {
          
              public static void main(String[] args) {
                  IntegerProperty integerProp = new SimpleIntegerProperty();
                  integerProp.addListener(new ChangeListener<Number>() {
          
                      @Override
                      public void changed(ObservableValue<? extends Number> observable,
                              Number oldValue, Number newValue) {
                          System.out.println("integerProperty changed value from "+oldValue+" to "+newValue);
                      }
                  });
                  
                  ObjectProperty<Integer> objectProp = new SimpleObjectProperty<>();
                  objectProp.addListener(new ChangeListener<Integer>() {
          
                      @Override
                      public void changed(ObservableValue<? extends Integer> observable,
                              Integer oldValue, Integer newValue) {
                          System.out.println("objectProperty changed value from "+oldValue+" to "+newValue);
                      }
                  });
                  
                  integerProp.setValue(null);
                  integerProp.setValue(1);
                  System.out.println("setting integerProp back to null:");
                  integerProp.setValue(null);
                  System.out.println("setting integerProp to 0:");
                  integerProp.setValue(0);
                  integerProp.setValue(null);
                  
                  objectProp.setValue(null);
                  objectProp.setValue(1);
                  objectProp.setValue(null);
                  objectProp.setValue(0);
                  objectProp.setValue(null);
                  
              }
          
          }
          
          • 2. Re: Properties: Handling of NULL values
            3a132ce1-ddfc-47dd-ac30-3e66c70c6b34

            Thank you for the reply.

            Yeah, i guess using "ObjectProperty" is the only possiblity if you have to deal with Integer values that can be NULL.