Java Bindings.isNotNull导致空指针异常

Java Bindings.isNotNull导致空指针异常,java,binding,javafx,Java,Binding,Javafx,我有一个简单的字段(实际上是一个属性): 我不明白的是,为什么运行这行代码时会出现NullPointerException。我知道有人打电话来,但我不明白为什么。难道不应该只在colored为非null时调用它吗?>难道不应该只在colored为非null时调用它吗 不。让我们对您的代码进行一些类比: SimpleObjectProperty<ObjectWithColor> colored = new SimpleObjectProperty<>(); colore

我有一个简单的字段(实际上是一个属性):

我不明白的是,为什么运行这行代码时会出现NullPointerException。我知道有人打电话来,但我不明白为什么。难道不应该只在colored为非null时调用它吗?

>难道不应该只在colored为非null时调用它吗

不。让我们对您的代码进行一些类比:

SimpleObjectProperty<ObjectWithColor> colored = new SimpleObjectProperty<>();  
colored.get().colorProperty();
SimpleObjectProperty<ObjectWithColor> colored = 
                     new SimpleObjectProperty<>(new ObjectWithColor(Color.RED));  
colored.get().colorProperty();
我们在getFullName().toString()处获得NullPointerException,因为getFullName()返回null

对于这两个比较,假设是:;包装字段没有默认值或未在默认构造函数中初始化

让我们继续这个假设。在这种情况下,我们可以通过,
通过构造函数初始化值:

colored = new SimpleObjectProperty<>();  
ObjectExpression<Color> color= Bindings.when(colored.isNotNull()).then(colored.get().colorProperty()). otherwise(Color.BLACK);
Person person = new Person("initial full name");
person.getFullName().toString();
或呼叫设置器:

Person person = new Person();
person.setFullName("Foo Bar");
person.getFullName().toString();
您的代码也是如此:

SimpleObjectProperty<ObjectWithColor> colored = new SimpleObjectProperty<>();  
colored.get().colorProperty();
SimpleObjectProperty<ObjectWithColor> colored = 
                     new SimpleObjectProperty<>(new ObjectWithColor(Color.RED));  
colored.get().colorProperty();
因此,语句
colored.get().colorProperty()
必须是可访问的。通常,可绑定的if-then-else块设计用于以下用途:

SimpleObjectProperty<Double> doubleProperty = new SimpleObjectProperty();
ObjectExpression<Double> expression = Bindings.when(doubleProperty.isNotNull()).then(doubleProperty).otherwise(-1.0);
System.out.println(doubleProperty.getValue() + "  " + doubleProperty.isNotNull().getValue() + "  " + expression.getValue());
doubleProperty.setValue(1.0);
System.out.println(doubleProperty.getValue() + "  " + doubleProperty.isNotNull().getValue() + "  " + expression.getValue());
因此,您可以定义初始默认值:

SimpleObjectProperty<ObjectWithColor> colored = new SimpleObjectProperty(new ObjectWithColor(Color.BLACK));
SimpleObjectProperty colored=新的SimpleObjectProperty(新的ObjectWithColor(Color.BLACK));
或者可以直接在绑定中使用ObjectWithColor.colorProperty。

>难道不应该仅在colored为非null时调用它吗

不。让我们对您的代码进行一些类比:

SimpleObjectProperty<ObjectWithColor> colored = new SimpleObjectProperty<>();  
colored.get().colorProperty();
SimpleObjectProperty<ObjectWithColor> colored = 
                     new SimpleObjectProperty<>(new ObjectWithColor(Color.RED));  
colored.get().colorProperty();
我们在getFullName().toString()处获得NullPointerException,因为getFullName()返回null

对于这两个比较,假设是:;包装字段没有默认值或未在默认构造函数中初始化

让我们继续这个假设。在这种情况下,我们可以通过,
通过构造函数初始化值:

colored = new SimpleObjectProperty<>();  
ObjectExpression<Color> color= Bindings.when(colored.isNotNull()).then(colored.get().colorProperty()). otherwise(Color.BLACK);
Person person = new Person("initial full name");
person.getFullName().toString();
或呼叫设置器:

Person person = new Person();
person.setFullName("Foo Bar");
person.getFullName().toString();
您的代码也是如此:

SimpleObjectProperty<ObjectWithColor> colored = new SimpleObjectProperty<>();  
colored.get().colorProperty();
SimpleObjectProperty<ObjectWithColor> colored = 
                     new SimpleObjectProperty<>(new ObjectWithColor(Color.RED));  
colored.get().colorProperty();
因此,语句
colored.get().colorProperty()
必须是可访问的。通常,可绑定的if-then-else块设计用于以下用途:

SimpleObjectProperty<Double> doubleProperty = new SimpleObjectProperty();
ObjectExpression<Double> expression = Bindings.when(doubleProperty.isNotNull()).then(doubleProperty).otherwise(-1.0);
System.out.println(doubleProperty.getValue() + "  " + doubleProperty.isNotNull().getValue() + "  " + expression.getValue());
doubleProperty.setValue(1.0);
System.out.println(doubleProperty.getValue() + "  " + doubleProperty.isNotNull().getValue() + "  " + expression.getValue());
因此,您可以定义初始默认值:

SimpleObjectProperty<ObjectWithColor> colored = new SimpleObjectProperty(new ObjectWithColor(Color.BLACK));
SimpleObjectProperty colored=新的SimpleObjectProperty(新的ObjectWithColor(Color.BLACK));

或者可以直接在绑定中使用ObjectWithColor.colorProperty。

如果不提供有关源代码的更多详细信息,您只能猜测发生了什么。显然,任何when()、then()、others()、get()或colorProperty()方法都可能返回null。我将从后两个开始。如果不提供更多关于源代码的细节,人们只能猜测发生了什么。显然,任何when()、then()、others()、get()或colorProperty()方法都可能返回null。我将从后两个开始。谢谢,实际上回复澄清了很多,但这是写给stackOverflow的一个打字错误。我的问题是,即使我说colored.isNotNull()时,出价也会变成colored.getColorProperty().get(),这会引发NullPointerException。正是我所需要的真的很烦人。谢谢我正在使用绑定。请选择。。。但我真的不喜欢这样。谢谢,实际上回复澄清了很多,但这是写给stackOverflow的一个打字错误。我的问题是,即使我说colored.isNotNull()时,出价也会变成colored.getColorProperty().get(),这会引发NullPointerException。正是我所需要的真的很烦人。谢谢我正在使用绑定。请选择。。。但我真的不喜欢这样。