Vaadin 24 - 使用 setItemLabelGenerator 的 SELECT 组件不允许 setEmptySelectionAllowed(true)

问题描述 投票:0回答:1

我有一个简单的选择组件,它充当过滤器并显示角色列表(返回角色的 ID):

private final Select<SJRolesQA> toCcBccFilter = new Select<>();
List<SJRolesQA> allRoles = rolesDataService.findAll();
toCcBccFilter.setItems(allRoles);
toCcBccFilter.setItemLabelGenerator(SJRolesQA::getRole);

效果很好 - 没有问题。

我想要做的是添加一个空值(返回 null 并显示“ALL”),以便用户可以撤消过滤器。我尝试添加:

toCcBccFilter.setEmptySelectionAllowed(true);
toCcBccFilter.setEmptySelectionCaption("ANY");

但是我遇到了 NullPointer 异常。错误如下:

java.lang.NullPointerException: null
    at com.vaadin.flow.component.select.Select.updateItem(Select.java:864) ~[vaadin-select-flow-24.1.3.jar:na]
    at com.vaadin.flow.component.select.Select.addEmptySelectionItem(Select.java:965) ~[vaadin-select-flow-24.1.3.jar:na]
    at com.vaadin.flow.component.select.Select.setEmptySelectionAllowed(Select.java:334) ~[vaadin-select-flow-24.1.3.jar:na]
    at edu.sjconfig.views.sjnotifications.SJNotificationsView.setToCcBccSelectValues(SJNotificationsView.java:152) ~[classes/:na]
    at edu.sjconfig.views.sjnotifications.SJNotificationsView.<init>(SJNotificationsView.java:85) ~[classes/:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:na]
    at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:77) ~[na:na]
    at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:499) ~[na:na]
    at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:480) ~[na:na]
vaadin vaadin-flow
1个回答
0
投票

我无法用给出的示例减去项目标签来重现这一点 发电机。添加“损坏的”项目标签生成器给了我一个类似的 错误,但更好——来自 Vaadin 的,我不允许使用 此处为空值。所以我的结论是,错误在于你的

SJRolesQA::getRole
抛出 NPE(如果没有,您还没有提供 完整的堆栈跟踪,带有“好的”Vaadin 消息)。

工作最小示例:

new Select<Map>().tap{
    setItemLabelGenerator {                 
        it?.keySet()?.toString() ?: "ALL"             
    }             
    setEmptySelectionAllowed(true)             
    setEmptySelectionCaption("ALL")             
    setItems((0..10).collect{                 
        [(it): it]             
    })             
    addValueChangeListener {                 
        Notification.show(it.toString())             
    }         
}
© www.soinside.com 2019 - 2024. All rights reserved.