Wicket 尝试在组件的空模型上设置模型对象

Wicket 尝试在组件的空模型上设置模型对象,wicket,Wicket,我是Wicket的新手,但谷歌搜索这个问题并没有给我任何有意义的东西。所以我希望有人能帮上忙 我有一个扩展表单的SiteChoice对象和一个扩展表单的SiteList对象 下拉选择。我的SiteChoice类看起来像: public class SiteChoice extends Form { public SiteChoice(String id) { super(id); addSiteDropDown(); } private v

我是Wicket的新手,但谷歌搜索这个问题并没有给我任何有意义的东西。所以我希望有人能帮上忙

我有一个扩展表单的SiteChoice对象和一个扩展表单的SiteList对象 下拉选择。我的SiteChoice类看起来像:

  public class SiteChoice extends Form {
     public SiteChoice(String id) {
        super(id);

    addSiteDropDown();
     }

  private void addSiteDropDown() {

    ArrayList<DomainObj> siteList = new ArrayList<DomainObj>();
   // add objects to siteList

    ChoiceRenderer choiceRenderer = new ChoiceRenderer<DomainObj>("name", "URL");

    this.add(new SiteList("siteid",siteList,choiceRenderer));
   }
}
我的Wicket模板有:

当我打开页面时,它呈现良好——下拉列表正确呈现。当我点击Submit时,出现了一个奇怪的错误:

WicketMessage: Method onFormSubmitted of interface 
  org.apache.wicket.markup.html.form.IFormSubmitListener targeted at component   
 [MarkupContainer [Component id = fittest]] threw an exception

Root cause:

   java.lang.IllegalStateException: Attempt to set model object on null 
model of component: testform:siteid
    at org.apache.wicket.Component.setDefaultModelObject(Component.java:3033)
    at
  org.apache.wicket.markup.html.form.FormComponent.updateModel(FormComponent.java:1168)
   at 
 [snip]

我不知道什么是空的。它渲染得很好,因此找到了对象。我遗漏了什么?

好吧,您没有显示SiteList类的代码,但实际情况是有些东西——几乎可以肯定是下拉列表——没有模型。所以当wicket调用时,本质上是,
dropdown.getModel().setModelObject(foo)它获取空指针异常

我的建议是,遵循旧的OO经验法则,更喜欢组合而不是继承。您的
SiteChoice
SiteList
类似乎没有添加太多内容,它们使您的错误更难调试

相反,只需在表单中添加一个下拉选项:

 form.add( new DropDownChioce( "siteid", 
                               new Model<DomainObject>(), 
                               new ChoiceRenderer<DomainObj>("name", "URL") );
form.add(新的DropDownChioce(“siteid”),
新模型(),
新的选秀者(“姓名”、“URL”);
这也更简洁

 form.add( new DropDownChioce( "siteid", 
                               new Model<DomainObject>(), 
                               new ChoiceRenderer<DomainObj>("name", "URL") );