Avatar: Error rendering label with only numbers
See original GitHub issueDescribe the bug
I have an issue where the value passed to the attribute label is only numbers or anything different as letters, the current version throws an exception with the message
java.lang.StringIndexOutOfBoundsException: String index out of range: 0 at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47) at java.base/java.lang.String.charAt(String.java:693) at org.primefaces.component.avatar.AvatarRenderer.calculateLabel(AvatarRenderer.java:128) at org.primefaces.component.avatar.AvatarRenderer.encodeEnd(AvatarRenderer.java:58) at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:595) at javax.faces.component.UIComponent.encodeAll(UIComponent.java:1654) at javax.faces.render.Renderer.encodeChildren(Renderer.java:152)
Reviewing the source code of the avatar component I found that the component is assuming that only letters are included in the label if not, the call of the following method throws the exception
protected String calculateLabel(FacesContext context, Avatar avatar) {
String value = avatar.getLabel();
if (value == null || value.length() <= 2) {
return value;
}
Matcher m = LETTER_PATTTERN.matcher(value);
StringBuilder sb = SharedStringBuilder.get(context, SB_AVATAR);
while (m.find()) {
sb.append(m.group());
}
String initials = sb.toString();
return initials.length() == 1 ? initials : initials.charAt(0) + initials.substring(initials.length() - 1);
}
Reproducer
No response
Expected behavior
The label should be taken as an string of characters no matters if its only numbers and not letters on it.
PrimeFaces edition
Elite
PrimeFaces version
11.0.9
Theme
Rain
JSF implementation
Mojarra
JSF version
2.2
Java version
11
Browser(s)
Chrome
Issue Analytics
- State:
- Created 10 months ago
- Comments:5 (4 by maintainers)

Top Related StackOverflow Question
OK with my latest fix your labels would come out like this…
Great let me add your use case to the unit tests.