vaadin / vaadin/framework

TreeData addItems causes NPE because it doesn't check childItems

Open
#11,400 4 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question Stale
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.