Skip to content
Advertisement

unchecked call to setValueFactory(SpinnerValueFactory)

I’m using Java JDK 17, and I get an unchecked call warning for the following code snippet:

Error

unchecked call to setValueFactory(SpinnerValueFactory)

Code

import javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory;

IntegerSpinnerValueFactory valueFactory;
valueFactory =  new IntegerSpinnerValueFactory(0,100, 0,1);
paramValue.setValueFactory(valueFactory);

How can I get rid of this warning.

Advertisement

Answer

Your problem is in the code you don’t provide, for these kinds of issues you should always try to provide a minimal example.

I have made an assumption about the missing code in this answer (the spinner declaration), hopefully that’s correct.

Fixing the warning by declaring generics

You need to properly declare generics on the spinner when it is created.

Let’s say you have a factory defined like this:

IntegerSpinnerValueFactory valueFactory =
        new IntegerSpinnerValueFactory(0, 100, 0, 1);

And you want to use it in a Spinner.

If you declare the spinner without generics, then you will get unchecked call warnings, both for when you declare and when you use the spinner (i.e. both of these lines cause warnings):

Spinner uncheckedSpinner = new Spinner();
uncheckedSpinner.setValueFactory(valueFactory);

If you declare the spinner with generics, then you will not get any unchecked call warnings:

Spinner<Integer> checkedSpinner = new Spinner<>();
checkedSpinner.setValueFactory(valueFactory);

General case for unchecked warnings

For more information see (and please read and try to understand):

This question could have been closed as a duplicate of the linked question, but I thought it best to provide a specific answer for your question case.

Also note that this is just a compiler warning, the code will still work (you lose some type safety compile-time checking, but as long as you code using the correct types, you wan’t get a runtime exception).

To understand generics better you should take time to re-visit a tutorial on them:

Supressing unchecked warnings

Sometimes, e.g. if using really old libraries or really complicated and odd generic libraries (e.g. the JavaFX TableView), it is nice to just suppress unchecked warnings, which can be done in Idea using the following method, which is probably available in some other way in other IDEs:

Of course, it is usually better to fix the underlying cause of the warning by appropriately declaring generics you use, but sometimes it is OK to just suppress them if you know exactly what you are doing.

Using a spinner constructor to provide the value factory

Also note that if you use the appropriate spinner constructor it is not necessary to provide a spinner value factory because the spinner implementation will be smart enough to choose and create the correct factory. So you could just skip the creation of the value factory yourself altogether if you just wrote:

Spinner<Integer> checkedSpinner = new Spinner<>(0, 100, 0);

Complete Example Code

import javafx.scene.Scene;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory;
import javafx.stage.Stage;

public class SpinnerChecks extends Application {
    @Override
    public void start(Stage stage) {
        IntegerSpinnerValueFactory valueFactory =
                new IntegerSpinnerValueFactory(0, 100, 0, 1);

        Spinner<Integer> checkedSpinner = new Spinner<>();
        checkedSpinner.setValueFactory(valueFactory);

        // both these lines will generate warnings upon compiler (and warning inside smart editors like Idea).
        // the compiler will output warning messages such as the following:
        //   SpinnerChecks.java
        //   java: SpinnerChecks.java uses unchecked or unsafe operations.
        //   SpinnerChecks.java
        //   java: Recompile with -Xlint:unchecked for details.
        Spinner uncheckedSpinner = new Spinner();
        uncheckedSpinner.setValueFactory(valueFactory);

        Spinner<Integer> constructedSpinner = new Spinner<>(0, 100, 0);

        stage.setScene(new Scene(checkedSpinner));
        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }
}
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement