Binding from Integer not working

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:

[code]
public class ItemEditor extends VerticalLayout {

TextField itemCount = new TextField(“ItemCount”);

Binder binder = new Binder<>(Item.class);
addComponents(itemCount, …);

binder.bindInstanceFields(this);
//Throwing exception!

[/code]
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:

[code]

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!

[/code]
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:

[code]
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);
[/code]
But I am binding it manually…
What gives!?

I am having the same problem too. Is it a bug or is there something I am not doing right

Would Like to know if u already found a solution to this problem

Same issue here. And not only here but at
StackOverflow: Does Vaadin 8 Binder::bindInstanceFields only work with String data types?
, too.

There’s issue
#8858 Binder.bindInstanceFields() overwrites existing bindings
.

Apparently a bug fix is in the works. While I see no target build/version due to get the fix being listed in in GitHub, I do indeed see the fixed source code appearing in
Vaadin 8.1.0 alpha 4 pre-release
.

See GitHub issue # 8998:
Make bindInstanceFields not bind fields already bound using functions #8998
. You can
see the source code changes
.

But I am confused. I do not know when this bug bites. You can see
my example code appended in the “Update” section of the Stack Overflow Question by Gerold Broser
that shows no problem, no bug in Vaadin 8.0.5 nor 8.1.0 alpha 3, where I establish a binding manually for a non-compatible data type (Integer) and then follow with a Binder::bindInstanceFields call to bind the rest of the fields. I’ve not determined the difference between my code example and the code of others complaining of the bug. At any rate, this may be moot if the fix is released soon in 8.x.

@Basil I know it gets confused almost every time, but it’s actually Ger

o

ld. :wink:

@Geri But I got it right on Stack Overflow recently, so I get half-credit. :slight_smile:

With a name like mine, I usually double-check the spelling of others.

And thanks for your helpful contributions.

I’m having a similar issue. I’m using 8.1.beta3.

I have an integer: if I put no converter, I have a ClassCastException: java.lang.Integer cannot be cast to java.lang.String. But when I add a StringToIntegerConverter I get java.lang.IllegalStateException: cannot modify binding: already bound to a property.
I’m not using the bindInstanceFields method, I bind each property manually and then I using readBean.

Any ideas?

Hi guys,

was the fix for this issue #8858 added to 8.0.6? On github it says it was added to the 8.0.6 milestone on 8 May but I have updated my application pom.xml with 8.0.6 version, recompiled and I still getting the exception.

I’m trying to save text from textfield to the int attribute by using:

binder.forField(maxlength)
.withConverter(
new StringToIntegerConverter(“Must enter a number”))
.bind(AlertField::getMaxLength, AlertField::setMaxLength);

Guys I think I have the solutuin for the error. What exactly causes the Error is the fact that for some reason the binder uses a different declared method rather than a the intended getter method.
For Example:

public class Item {
private Integer itemCount;
//uses   
private boolean isItemValid(){
 return true;
}
//should use
private getitemCount(){
return itemCount;
}
}

You need to change that method name to somthing more unique or different than the original getter. That worked for me somehow.

still not working in 8.4.0, how something so basic doesn’t work properly yet?

binder.forField(id).withConverter(new StringToIntegerConverter("id.....?")).getField().setReadOnly(true);

the workaround is
binder.bind(id, o -> o.getId() + "", null);

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

Same problem here, really annoying…

This seems to be working OK for me, can someone point out how I can demonstrate the problem from here:


    public static class Item {
        public Integer getItemCount() {
            return itemCount;
        }

        public void setItemCount(Integer itemCount) {
            this.itemCount = itemCount;
        }

        private Integer itemCount;
    }


    @Override
    protected void init(VaadinRequest vaadinRequest) {
        final VerticalLayout layout = new VerticalLayout();
        TextField itemCountTextField = new TextField("Item count", "0");
        Item item = new Item();
        item.setItemCount(0);

        Binder<Item> itemBinder = new Binder<>();
        itemBinder.forField(itemCountTextField)
                .withConverter(new StringToIntegerConverter("must be integer"))
                .bind(Item::getItemCount, Item::setItemCount);
        itemBinder.readBean(item);

        Button button = new Button("check count from item", e -> {
            try {
                itemBinder.writeBean(item);
                Notification.show("Current count: " + item.getItemCount());
            } catch (ValidationException ve) {
                Notification.show("ValidationException: " + ve.getMessage());
            }

        });
        layout.addComponents(itemCountTextField, button);
        setContent(layout);
    }

maven compilation is no longer possible since version 8.7.2. I still get incompatible types: bad return type in method reference
[ERROR]
java.lang.String can not be converted to java.lang.Double

        Grid.Column<GarantieJson, Double> c3 = addColumn(GarantieJson::getPrixPlusDeTroisTonnesCinq)
                .setCaption(">3T5")
                .setMinimumWidthFromContent(false)
                .setSortable(true)
                .setEditorBinding(
                        garantieJsonBinder.forField(new TextField())
                        .withConverter(new StringToDoubleConverter(0.0,"not a double"))
                        .bind(GarantieJson::getPrixPlusDeTroisTonnesCinq,GarantieJson::setPrixPlusDeTroisTonnesCinq)
                );