代码之家  ›  专栏  ›  技术社区  ›  MikeHoss

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

  •  4
  • MikeHoss  · 技术社区  · 15 年前

    我对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));
       }
    }
    

    然后我只需将SiteChoice对象添加到页面对象a la:

        SiteChoice form = new SiteChoice("testform");
        add(form);
    

    我的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]
    

    我不知道什么是空的。它渲染得很好,所以找到了对象。我错过了什么?

    1 回复  |  直到 15 年前
        1
  •  12
  •   tpdi    15 年前

    好吧,您没有显示SiteList类的代码,但是发生的事情是——几乎可以肯定是下拉列表——没有模型。所以当威克特打电话的时候, dropdown.getModel().setModelObject( foo ) ; 它得到一个空指针异常。

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

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

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

    这也更简洁了,