PDA

View Full Version : index out of bounds error with NumberField.setAllowedDecimals(false)



nbuesing
27 Dec 2011, 2:33 PM
I create a NumberField<BigDecimal> (with my own BigDecimalPropertyEditor) I defined. If I call field.setAllowDecimal(false) I get an index out of bounds error (index 46 of 12). When looking at the code (in red), the decimalSeparator.charAt(i) returns a 'char' not a Character, so allowed.remove(int index) is being called instead of allowed.remove(Character character).

Looks like the add code would have similar issues, but since the characters are already contained, the add never gets called.

I believe allowed.remove(Character.valueOf(decimalSeparator.charAt(i))); (and same for allowed.add) is needed.


/**
* Sets whether decimal value are allowed (defaults to true).
*
* @param allowDecimals true to allow negative values
*/
public void setAllowDecimals(boolean allowDecimals) {
this.allowDecimals = allowDecimals;
if (allowDecimals) {
for (int i = 0; i < decimalSeparator.length(); i++) {
if (!allowed.contains(decimalSeparator.charAt(i))) {
allowed.add(decimalSeparator.charAt(i));
}
}
} else {
for (int i = 0; i < decimalSeparator.length(); i++) {
allowed.remove(decimalSeparator.charAt(i));
}
}
}