Exception when trying to update an int value from the client to the model in Java
See original GitHub issueIf a property is defined as Number in client template (.html) and it tries to update its conterpart field in the Java Model that is also declared as a supported type, as an int, I get this exception:
Caused by: java.lang.IllegalArgumentException: The stored model value '1' type 'java.lang.String' cannot be used as a type for a model property with type 'int'
at com.vaadin.flow.templatemodel.BasicModelType.modelToApplication(BasicModelType.java:64)
Source code used to reproduce the error:
<dom-module id="x-inter">
<template>
<input type="radio" name="toggle" id="id1" value= 1 on-change="_getSelected">One
<input type="radio" name="toggle" id="id2" value= 2 on-change="_getSelected">Two
</template>
<script>
class XInter extends Polymer.Element{
static get is(){
return "x-inter";
}
static get properties(){
return {
selectedIndex: {
type: Number,
reflectToAttribute: true,
notify: true
}
};
}
_getSelected(){
if(this.shadowRoot.querySelector('input[name=toggle]:checked')){
this.selectedIndex = this.shadowRoot.querySelector('input[name=toggle]:checked').value;
}
else
this.selectedIndex = -1;
this.dispatchEvent(new CustomEvent('change', {}));
}
}
customElements.define(XInter.is, XInter);
</script>
</dom-module>
<dom-module id="x-element">
<template>
<x-inter selected-index= {{selectedValue}} on-change="_valueChanged"></x-inter>
</template>
<script>
class XElement extends Polymer.Element{
static get is(){
return "x-element";
}
static get properties(){
return {
selectedValue:{
type: Number,
notify: true
}
};
}
}
customElements.define(XElement.is, XElement);
</script>
</dom-module>
@Tag("x-element")
@HtmlImport("x-element.html")
public class XElement extends PolymerTemplate<Model> {
public XElement() {
this.getElement().synchronizeProperty("selectedValue", "_valueChanged");
this.getElement().addPropertyChangeListener("selectedValue", e -> {
System.out.println(getModel().getSelectedValue());
});
}
public interface Model extends TemplateModel {
int getSelectedValue();
void setSelectedValue(int value);
}
}
Issue Analytics
- State:
- Created 5 years ago
- Comments:11 (11 by maintainers)
Top Results From Across the Web
How to Handle the Incompatible Types Error in Java - Rollbar
The Java incompatible types error happens when a value assigned to a variable or returned by a method is incompatible with the one...
Read more >java: unable to cast an Integer object to String - Stack Overflow
No, it doesn't. There's no way calling toString() on an object causes a ClassCastException like the one you have. Again, stop using Object[]...
Read more >Java Exceptions And Exception Handling With Examples
An exception handler interrogates the context at the point when the exception occurred. This means that it reads the variable values that were ......
Read more >10 Reasons of java.lang.NumberFormatException in ... - Java67
This error comes when you try to convert a String into numeric data types e.g., int, float, double, long, short, char, or byte....
Read more >Exception Handling in Java - DigitalOcean
Sometimes we might want to generate exceptions explicitly in our code. For example, in a user authentication program, we should throw exceptions ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Seems like I was mistaken. Flow does in fact not automatically handle conversion from JS number to
int
in this case, so this is something that we should fix.Until it is fixed, you can work around the problem using the suggested workaround of changing the model type to
double
instead ofint
.isNaN is not guarantee that value is number if it returns false. It checks that value can be converted to number or nor, so isNaN(“1”) is false.