Cannot cast `java.util.Collections$EmptySet` to `java.util.HashSet`.

huangapple go评论105阅读模式
英文:

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

实例化和使用组合框效果很好,但是在尝试“保存”没有选择任何项目的对象时,我遇到了错误

  1. java.lang.RuntimeException: java.lang.ClassCastException: Cannot cast java.util.Collections$EmptySet to java.util.HashSet

如果至少选择了一个项目,它就可以正常工作,但是一旦选择为空并尝试保存对象(“AnotherClass”),我就会收到错误。

  1. // 创建组合框
  2. private MultiselectComboBox<MyClass> multiselectComboBox;
  3. multiselectComboBox = new MultiselectComboBox<>();
  4. // 设置可供选择的项目
  5. final MyClassDataProvider dataProvider = new MyClassDataProvider();
  6. List<MyClass> allAvailableOptions = new ArrayList<>(dataProvider.getItems());
  7. multiselectComboBox.setItems(allAvailableOptions);
  8. multiselectComboBox.setItemLabelGenerator(MyClass::getName); // 仅显示名称
  9. // 将组合框绑定到 AnotherClass 的字段
  10. binder = new BeanValidationBinder<>(AnotherClass.class);
  11. binder.forField(multiselectComboBox)
  12. .bind("myHashSet");
  13. // 保存按钮
  14. save = new Button("Save");
  15. save.addClickListener(event -> {
  16. if (currentObject != null && binder.writeBeanIfValid(currentObject)) { // 此行出错
  17. viewLogic.saveRisk(currentObject);
  18. }
  19. });

HashSet 是以下类中的一个属性:

  1. public class AnotherClass implements Serializable {
  2. @NotNull
  3. private int id = -1;
  4. private HashSet<MyClass> myHashSet = new HashSet<MyClass>();
  5. }

当我创建 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

  1. 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.

  1. // creating a combobox
  2. private MultiselectComboBox&lt;MyClass&gt; multiselectComboBox;
  3. multiselectComboBox= new MultiselectComboBox&lt;&gt;();
  4. // setting items to choose from
  5. final MyClassDataProvider dataProvider = new MyClassDataProvider();
  6. List&lt;MyClass&gt; allAvailableOptions = new ArrayList&lt;&gt;(dataProvider.getItems());
  7. multiselectComboBox.setItems(allAvailableOptions);
  8. multiselectComboBox.setItemLabelGenerator(MyClass::getName); // display name only
  9. // binding the combobox to a field of AnotherClass
  10. binder = new BeanValidationBinder&lt;&gt;(AnotherClass.class);
  11. binder.forField(multiselectComboBox)
  12. .bind(&quot;myHashSet&quot;);
  13. // save-button
  14. save = new Button(&quot;Save&quot;);
  15. save.addClickListener(event -&gt; {
  16. if (currentObject!= null
  17. &amp;&amp; binder.writeBeanIfValid(currentObject)) { // error in this line
  18. viewLogic.saveRisk(currentObject);
  19. }
  20. });

The HashSet is an attribute in the following class:

  1. public class AnotherClass implements Serializable {
  2. @NotNull
  3. private int id = -1;
  4. private HashSet&lt;MyClass&gt; myHashSet= new HashSet&lt;MyClass&gt;();
  5. }

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文档)。

  1. public class AnotherClass implements Serializable {
  2. @NotNull
  3. private int id = -1;
  4. private Collection<MyClass> myHashSet = new HashSet<MyClass>();
  5. }
英文:

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).

  1. public class AnotherClass implements Serializable {
  2. @NotNull
  3. private int id = -1;
  4. private Collection&lt;MyClass&gt; myHashSet= new HashSet&lt;MyClass&gt;();
  5. }

huangapple
  • 本文由 发表于 2020年4月6日 20:35:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/61060007.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定