Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MaterialListBox#getIndex(String value) throws IndexOutOfBoundsException #899

Closed
rodrigoborgesdeoliveira opened this issue Mar 14, 2019 · 1 comment

Comments

@rodrigoborgesdeoliveira
Copy link
Contributor

rodrigoborgesdeoliveira commented Mar 14, 2019

If the MaterialListBox has an empty placeholder, the following code

public int getIndex(T value) {
        int count = getItemCount();
        for (int i = 0; i < count; i++) {
            if (Objects.equals(getValue(i), value)) {
                return i;
            }
        }
        return -1;
    }

throws an IndexOutOfBoundsException at getValue(i).
The reason is that the getItemCount() will also count the empty placeholder as an item, so when calling getValue(i) in the for loop, it will then call getValueInternal(index + getIndexOffset()), which will, in the last element, try to get a position = count.
Example:
If the list has no items, count will be 1 and getValue(0) will translate to getValueInternal(1). Since there is only one element (index 0), it will throw the exception.

@kevzlou7979
Copy link
Contributor

Merged via 6804e9f. Again thanks for your contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants