question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Avatar: Error rendering label with only numbers

See original GitHub issue

Describe 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:closed
  • Created 10 months ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
mellowarecommented, Nov 30, 2022

OK with my latest fix your labels would come out like this…

                   Arguments.of("41235 5asdd", "4"),
                    Arguments.of("1234", "1"),
                    Arguments.of("888 7666", "8"),
1reaction
mellowarecommented, Nov 30, 2022

Great let me add your use case to the unit tests.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why is QGIS only one label rendering at a time?
It turns out it was not a rendering issue, but a data source issue. The layer source was a database and the unique...
Read more >
How to allow only numbers in textbox in reactjs?
Show activity on this post. Basic idea is: Use controlled component (use value and onChange property of input field), and inside onChange handle...
Read more >
Why the number input is the worst input - Stack Overflow Blog
* We do have an input that's labeled as a number, but when you add it to your form, it actually renders as...
Read more >
React Avatar component - Material UI - MUI
AvatarGroup renders its children as a stack. Use the max prop to limit the number of avatars. +2. Cindy ...
Read more >
Understanding the "Objects are not valid as a react child" Error ...
React has no way to tell what to render when provided with an object, thus the Invariant Violation error pops up when attempting...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found