Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problem with using IntegerField (strange ClassCastException) #136

Open
mgodbole opened this issue Aug 25, 2024 · 1 comment
Open

Problem with using IntegerField (strange ClassCastException) #136

mgodbole opened this issue Aug 25, 2024 · 1 comment

Comments

@mgodbole
Copy link

mgodbole commented Aug 25, 2024

Hi!
I have just started to learn Vaadin, so please bear with me.

I am using GridCrud (awesome add on @alejandro-du , thank you!)
In the editor, I wish to use the IntegerField as I love the UI (steps, helper text etc).
But I am getting a strange (to me at least) ClassCastException.

Here are the details:

The Entity

@Entity
public class Movie {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @NotNull(message = "Movie name required")
    @NotBlank(message = "Movie name required")
    private String name;

    @NotNull(message = "Rating required")
    @Min(value = 0, message = "Rating should be between 0-10")
    @Max(value = 10, message = "Rating should be between 0-10")
    private Integer rating;

    public Movie() {
	// TODO Auto-generated constructor stub
    }

    public Movie(String name, Integer rating) {
	this.name = name;
	this.rating = rating;
    }
    
    //Getter setter etc omitted
}

The view

@Route(value = "")
@PageTitle("GridCrud Test")
public class MainView extends VerticalLayout {

    public MainView(MovieService service) {
	var crud = new GridCrud<>(Movie.class, service);

	// Extract form
	var form = crud.getCrudFormFactory();
	form.setUseBeanValidation(true);

	// Customize form
	form.setVisibleProperties("name", "rating");

	form.setFieldProvider("rating", movie -> {
	    var ratingField = new IntegerField("Rating");
	    ratingField.setMin(0);
	    ratingField.setMax(10);
	    ratingField.setStepButtonsVisible(true);
	    ratingField.setHelperText("Rating out of 10");

	    return ratingField;
	});

	setSizeFull();
	add(new H2("Movie database"), crud);
    }

}

The UX

  1. When I click on the "Add" button, the editor pops up and the IntegerField shows up correctly, but after entering the values (validation runs correctly) I get the ClassCastException.
  2. On the other hand if I click on the "Edit" button, the editor does not show at all! (Same exception)

com.vaadin.flow.data.binder.BindingException: An exception has been thrown inside binding logic for the field element [label='Rating']

Caused by: java.lang.ClassCastException: class java.lang.String cannot be cast to class java.lang.Number (java.lang.String and java.lang.Number are in module java.base of loader 'bootstrap')
at com.vaadin.flow.component.textfield.AbstractNumberField.setValue(AbstractNumberField.java:40) ~[vaadin-text-field-flow-24.4.9.jar:na]
at com.vaadin.flow.data.binder.Binder$BindingImpl.lambda$convertAndSetFieldValue$4(Binder.java:1572) ~[flow-data-24.4.6.jar:24.4.6]
at com.vaadin.flow.data.binder.Binder$BindingImpl.execute(Binder.java:1673) ~[flow-data-24.4.6.jar:24.4.6]

Looks like the issue is with setValue which is not making sense to me.
Any pointers to help guide me in the right direction?

Thank you

1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants
@mgodbole and others