TreeGrid client side IndexOutOfBoundsException after expanding new node
After adding a new root and child node to the tree, and expanding the new root node, a client side exception is thrown. For example, when adding the items to an empty tree, this exception is thrown:
SEVERE: Error performing server to client RPC calls
java.lang.IndexOutOfBoundsException: The given index (1) was outside of the current number of rows (0..0)
After this exception, the grid refreshes its entire content (dropRows, and requestRows RPC calls), and recovers (so the exception is not visible on the UI, only in console).
Vaadin version: 8.1.4
Code sample to reproduce:
public class MyUI extends UI {
@Override
protected void init(VaadinRequest vaadinRequest) {
TreeGrid<Item> tree = new TreeGrid<>(Item.class);
Button button = new Button("Add new item", e -> addItem(tree));
setContent(new VerticalLayout(tree, button));
}
private void addItem(TreeGrid<Item> tree) {
Item parent = new Item("Parent");
Item child = new Item("Child");
TreeData<Item> treeData = tree.getTreeData();
treeData.addItem(null, parent);
treeData.addItem(parent, child);
tree.getDataProvider().refreshAll();
tree.expand(parent);
}
public static class Item {
private String name;
public Item(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
@WebServlet(urlPatterns = "/*", name = "MyUIServlet", asyncSupported = true)
@VaadinServletConfiguration(ui = MyUI.class, productionMode = false)
public static class MyUIServlet extends VaadinServlet {
}
}
Hello there!
It looks like this issue hasn't progressed lately. There are so many issues that we just can't deal them all within a reasonable timeframe.
There are a couple of things you could help to get things rolling on this issue (this is an automated message, so expect that some of these are already in use):
- Check if the issue is still valid for the latest version. There are dozens of duplicates in our issue tracker, so it is possible that the issue is already tackled. If it appears to be fixed, close the issue, otherwise report to the issue that it is still valid.
- Provide more details how to reproduce the issue.
- Explain why it is important to get this issue fixed and politely draw others attention to it e.g. via the forum or social media.
- Add a reduced test case about the issue, so it is easier for somebody to start working on a solution.
- Try fixing the issue yourself and create a pull request that contains the test case and/or a fix for it. Handling the pull requests is the top priority for the core team.
- If the issue is clearly a bug, use the Warranty in your Vaadin subscription to raise its priority.
Thanks again for your contributions! Even though we haven't been able to get this issue fixed, we hope you to report your findings and enhancement ideas in the future too!
I can still reproduce it with vaadin version 8.3.2.
A work around is to do the following:
Instead of using tree.expand(parent), use tree.getDataCommunicator().expand(parent, false);
tree.expand calls tree.getDataCommunicator().expand(parent, true), which is telling Vaadin to refresh an existing item. tree.getDataCommunicator().expand(parent, false) just marks the item as expanded, which is fine as the item is new and gets communicated as expanded.
First of all, please provide hashcode/equals for your Item and check if it works or not
I can confirm this occurs with an equals and hashCode defined.
public static class Item {
private final String name;
public Item(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public boolean equals(Object obj) {
if (obj == this) return true;
if (obj instanceof Item) {
Item otherItem = (Item) obj;
return Objects.equals(this.name, otherItem.name);
}
return false;
}
@Override
public int hashCode() {
return Objects.hash("item", name);
}
}
This problem only arises when adding and expanding items to a tree already resident on the client side.
If you look at TreeGrid.expand(Collection):
public void expand(Collection<T> items) {
HierarchicalDataCommunicator<T> communicator = getDataCommunicator();
items.forEach(item -> {
if (!communicator.isExpanded(item)
&& communicator.hasChildren(item)) {
communicator.expand(item); // includes client side sync
fireExpandEvent(item, false);
}
});
}
communicator.expand(item, false) should to be called for items that have not been transmitted to the client yet, or just schedule the sync on the client to run after new items been added.