英文:
Cannot cast java.util.Collections$EmptySet to java.util.HashSet
问题
我正在使用 Vaadin 14 和 Java 1.8。我想要实现一个多选组合框,所以我正在使用以下 Vaadin 插件: https://vaadin.com/directory/component/multiselect-combo-box/api/org/vaadin/gatanaso/MultiselectComboBox.html
实例化和使用组合框效果很好,但是在尝试“保存”没有选择任何项目的对象时,我遇到了错误
java.lang.RuntimeException: java.lang.ClassCastException: Cannot cast java.util.Collections$EmptySet to java.util.HashSet
如果至少选择了一个项目,它就可以正常工作,但是一旦选择为空并尝试保存对象(“AnotherClass”),我就会收到错误。
// 创建组合框
private MultiselectComboBox<MyClass> multiselectComboBox;
multiselectComboBox = new MultiselectComboBox<>();
// 设置可供选择的项目
final MyClassDataProvider dataProvider = new MyClassDataProvider();
List<MyClass> allAvailableOptions = new ArrayList<>(dataProvider.getItems());
multiselectComboBox.setItems(allAvailableOptions);
multiselectComboBox.setItemLabelGenerator(MyClass::getName); // 仅显示名称
// 将组合框绑定到 AnotherClass 的字段
binder = new BeanValidationBinder<>(AnotherClass.class);
binder.forField(multiselectComboBox)
.bind("myHashSet");
// 保存按钮
save = new Button("Save");
save.addClickListener(event -> {
if (currentObject != null && binder.writeBeanIfValid(currentObject)) { // 此行出错
viewLogic.saveRisk(currentObject);
}
});
HashSet 是以下类中的一个属性:
public class AnotherClass implements Serializable {
@NotNull
private int id = -1;
private HashSet<MyClass> myHashSet = new HashSet<MyClass>();
}
当我创建 AnotherClass 的实例时,我总是将其实例化为空的 HashSet,而不是将其实例化为 null。
如何修复上述错误?
英文:
I'm using Vaadin14 and Java 1.8. I want to implement a multi-select combobox, which is why I am using the following Vaadin addon: https://vaadin.com/directory/component/multiselect-combo-box/api/org/vaadin/gatanaso/MultiselectComboBox.html
Instantiating and using the combobox works great, but I'm getting the error
java.lang.RuntimeException: java.lang.ClassCastException: Cannot cast java.util.Collections$EmptySet to java.util.HashSet
when trying to "save" an object without any items selected in the combobox. If I have at least one item selected, it works fine, but as soon as the selection is empty and I try to save the object ("AnotherClass"), i receive the error.
// creating a combobox
private MultiselectComboBox<MyClass> multiselectComboBox;
multiselectComboBox= new MultiselectComboBox<>();
// setting items to choose from
final MyClassDataProvider dataProvider = new MyClassDataProvider();
List<MyClass> allAvailableOptions = new ArrayList<>(dataProvider.getItems());
multiselectComboBox.setItems(allAvailableOptions);
multiselectComboBox.setItemLabelGenerator(MyClass::getName); // display name only
// binding the combobox to a field of AnotherClass
binder = new BeanValidationBinder<>(AnotherClass.class);
binder.forField(multiselectComboBox)
.bind("myHashSet");
// save-button
save = new Button("Save");
save.addClickListener(event -> {
if (currentObject!= null
&& binder.writeBeanIfValid(currentObject)) { // error in this line
viewLogic.saveRisk(currentObject);
}
});
The HashSet is an attribute in the following class:
public class AnotherClass implements Serializable {
@NotNull
private int id = -1;
private HashSet<MyClass> myHashSet= new HashSet<MyClass>();
}
When I create instances of AnotherClass I always instantiate them not with null but with an empty HashSet for the attribute myHashSet.
How can I fix the error above?
答案1
得分: 0
在尝试了@Rogue在评论中给我的提示后,我首先尝试了使用Set<>,但最终奏效的是在类定义中向上再抽象一层,使用Collection<>而不是HashSet<>(Oracle文档)。
public class AnotherClass implements Serializable {
@NotNull
private int id = -1;
private Collection<MyClass> myHashSet = new HashSet<MyClass>();
}
英文:
After trying the hint @Rogue gave me in the comments, I tried it with Set<> first, but what finally did the trick was going another level of abstraction upwards and using Collection<> instead of HashSet<> in the class definition (Oracle Docs).
public class AnotherClass implements Serializable {
@NotNull
private int id = -1;
private Collection<MyClass> myHashSet= new HashSet<MyClass>();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论