Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Binding from Integer not working

B C
5 years ago Mar 09, 2017 6:58pm

I'm basically following a tutorial on setting up CRUD editor form from a grid of the table results.  Everything works with creating TextFields for Strings, but then it throws an IllegalStateException when trying to bind to an element that is an Integer type.  (If I simply comment out that one field from the editor form, everything works as expected.)  I'm using vaadin-bom 8.0.0.

I have an editor class:

public class ItemEditor extends VerticalLayout {
...
TextField itemCount = new TextField("ItemCount");
...
Binder<Item> binder = new Binder<>(Item.class);
addComponents(itemCount, ...);

binder.bindInstanceFields(this); [b]//Throwing exception![/b]

Where Item has an Integer field itemCount;

public class Item {
   private Integer itemCount;
   ...
}

So I look around and find that I have to manually set up a binder for non-string items to TextField, even for the basic primitave types.

OK, no big deal, so I then manually add this:

...
binder.forField(itemCount)
                .withConverter(new StringToIntegerConverter(""))
                //.withConverter(Integer::valueOf, String::valueOf)
                .bind(Item::getItemCount, Item::setItemCount);

addComponents(itemCount, ...);

binder.bindInstanceFields(this); //Still throwing exception on the itemCount field!
...

Results in:
java.lang.IllegalStateException: Property type 'java.lang.Integer' doesn't match the field type 'java.lang.String'. Binding should be configured manually using converter.

Quote from the documentation:

We can also bind some of the fields before calling bindInstanceFields. In this way, fields that require special configuration can still be configured manually while regular fields can be configured automatically.

binder.forField(yearOfBirth)
  .withConverter(
    new StringToIntegerConverter("Please enter a number"))
  .bind(Person::getYearOfBirth, Person::setYearOfBirth));

binder.bindInstanceFields(this);

But I am binding it manually... What gives!?

Kingsley Amaeze
5 years ago Mar 24, 2017 12:31pm
Kingsley Amaeze
5 years ago Mar 24, 2017 2:19pm
Gerold Broser
5 years ago Apr 02, 2017 12:05am
Basil Bourque
5 years ago Apr 10, 2017 3:44am
Gerold Broser
5 years ago Apr 11, 2017 5:55pm
Basil Bourque
5 years ago Apr 11, 2017 9:50pm
Josefina R
4 years ago Jun 28, 2017 9:45pm
Martin Petrik
4 years ago Jul 08, 2017 8:17pm
David Fischer
4 years ago Feb 21, 2018 9:54am
jose luis romero
3 years ago May 11, 2018 2:09am
Sebastien Libert
3 years ago Dec 15, 2018 9:15am

Hello guys,

Can someone from vaadin team shed some light on this one ? I just tested 8.6.3, and issue is still there. Workaround is a real pain, especially when you use some factory to generate and bind fields...

Regards

Last updated on Dec, 15th 2018
Klug Gauvain
3 years ago Dec 17, 2018 9:23am
Olli Tietäväinen
3 years ago Dec 17, 2018 11:40am
Guillaume Hochart
2 years ago Apr 18, 2019 8:51am