java.lang.ClassCastException:java.util.ArrayList

时间:2011-06-24 11:14:35

标签: java junit wicket

我正在运行Junit测试,收到错误:

GroupOptionInputPanelTest.java:

public void setUp() {           
        groupOptionInputPanel = new GroupOptionInputPanel(TEST_ID, new Model(new ArrayList()), null);
    }

GroupOptionInputPanel.java:

public GroupOptionInputPanel(String id, IModel model, final Form parentForm) {
        this(id, model, parentForm, false);
    }

...

ListView options = new GroupElementListView("options", getSelectionGroup().getSelectionOptions(), getInitTabIndex()) {

...

public abstract class AbstractOptionPanel extends Panel {
public SelectionGroup getSelectionGroup() {
        return (SelectionGroup) getModel().getObject();
    }

错误:

java.lang.ClassCastException: java.util.ArrayList
    at com.bgc.ordering.wizard.front.groupoptions.AbstractOptionPanel.getSelectionGroup(AbstractOptionPanel.java:33)
    at com.bgc.ordering.wizard.front.groupoptions.GroupOptionInputPanel.<init>(GroupOptionInputPanel.java:54)
    at com.bgc.ordering.wizard.front.groupoptions.GroupOptionInputPanel.<init>(GroupOptionInputPanel.java:47)

如何解决此错误?

public GroupOptionInputPanel(String id, IModel model, final Form parentForm, final boolean canDisplayExclamationMark) {
        super(id, model);
        this.setOutputMarkupId(true);

        ListView options = new GroupElementListView("options", getSelectionGroup().getSelectionOptions(), getInitTabIndex()) {


  public Model(Serializable object)
    {
        setObject(object);
    }

模型是Wicket API。

1 个答案:

答案 0 :(得分:1)

您的模型包含ArrayList个实例,并且您正尝试将其强制转换为SelectionGroupnew Model(new SelectionGroup())应该修复它。