Skip to content
Advertisement

JavaFX’s Spinner raises NullPointerException on empty text input

I have an issue where an editable JavaFX 8 Spinner causes an uncaught NullPointerException if one clears the editor text and commits and then clicks either the increment or decrement button. This is j8u60 j8u77. With some luck the increment/decrement button will get stuck in depressed state and the NPE’s keep flowing locking up the application.

The following code reproduces the issue for me:

JavaScript

Run it, clear the text, press enter (NullPointerException), clicking either increment or decrement button will now also cause NPE.

Can any one confirm that this is a JavaFX bug and suggest a workaround?

Edit: The exception stack trace

JavaScript

Advertisement

Answer

I had a rummage through the JDK source.

The NPE is thrown from if (newValue < getMin()) { in the listener lambda here:

javafx.scene.control.SpinnerValueFactory.java

JavaScript

presumably newValue is null and the auto unboxing of null throws NPE. As the input comes from the editor, I suspect the IntegerStringConverter which is the default converter.

Looking at the implementation here:

javafx.util.converter.IntegerStringConverter

JavaScript

We see that it will happily return null for the empty string, which is kind of reasonable given that there exists no valid value for the input.

Tracing up the call stack I find where the value is coming from:

javafx.scene.control.Spinner

JavaScript

The value is set with the value obtained from the converter T value = converter.fromString(text); which presumably is null. At this point I believe that the spinner class should check that value is not null and if it is restore the previous value to the editor.

I am now fairly sure that this is a bug. Moreover I don’t think that a work around with a converter that never returns null is going to work properly as it will only mask the problem and what value should be returned when the value cannot be converted?

Edit: Workaround

Replacing the onAction of the spinner editor to reject invalid input with a “return to valid” policy fixes the issue:

JavaScript

As opposed to a listener on the valueProperty this avoids triggering other listeners with invalid data. However this highlights another issue in the spinner class. While the above fixes the issue by returning to a valid value on pressing enter. Erasing the input without committing (pressing enter) and then pressing increment or decrement will cause the same NPE but with slightly different call stack.

Cause:

JavaScript

Decrement is similar, both call into commitEditorText below:

JavaScript

Notice the copy-paste from the onAction in the constructor:

JavaScript

I believe that commitEditorText should be changed to trigger onAction on the editor instead like so:

JavaScript

then the behavior would be consistent and give the editor a chance to handle the input before it goes to the value factory.

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement