TreeData addItems causes NPE because it doesn't check childItems
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 1.8k
- Forks
- 717
- Avg merge
- 2d 6h
- Merged PRs (30d)
- 3
Description
The TreeData.addItems() method below can cause a NullPointerException if any item in the tree has no children.
public TreeData<T> addItems(Collection<T> rootItems,
ValueProvider<T, Collection<T>> childItemProvider) {
rootItems.forEach(item -> {
addItem(null, item);
Collection<T> childItems = childItemProvider.apply(item);
addItems(item, childItems);
addItemsRecursively(childItems, childItemProvider);
});
return this;
}
The code should check "childItems" to make sure its not null prior to calling addItems(item, childItems). e.g.
public TreeData<T> addItems(Collection<T> rootItems,
ValueProvider<T, Collection<T>> childItemProvider) {
rootItems.forEach(item -> {
addItem(null, item);
Collection<T> childItems = childItemProvider.apply(item);
if (childItems != null) {
addItems(item, childItems);
addItemsRecursively(childItems, childItemProvider);
}
});
return this;
}
My calling code:
TreeData<UserGroupRow> data = new TreeData<>();
if (groups != null)
data.addItems(groups, UserGroupRow::getUserGroupChildren);
In this code some values returned by getUserGroupChildren may be null.
Vaadin Framework 8.63
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start at the TreeData.addItems() entry point shown in the issue and trace how a null result from the childItemProvider is handled. Reproduce the case with an item whose children are null, then verify that traversal no longer throws a NullPointerException and that non-null child collections are still processed.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100