2 Replies Latest reply on Sep 27, 2013 3:56 PM by 3a132ce1-ddfc-47dd-ac30-3e66c70c6b34

    Properties: Handling of NULL values


      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.



      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

          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>() {
                      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>() {
                      public void changed(ObservableValue<? extends Integer> observable,
                              Integer oldValue, Integer newValue) {
                          System.out.println("objectProperty changed value from "+oldValue+" to "+newValue);
                  System.out.println("setting integerProp back to null:");
                  System.out.println("setting integerProp to 0:");
          • 2. Re: Properties: Handling of NULL values

            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.