Attempt to set model object on null model of component

蓝咒 提交于 2019-12-10 17:36:49

问题


I'm new to Wicket, but googling this problem didn't give me anything that made sense. So I'm hoping someone in SO can help.

I have a SiteChoice object that extends Form, and a SiteList object that extends DropDownChoice. My SiteChoice class looks like:

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

Then I simply add my SiteChoice object to my Page object a la:

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

My Wicket template has:

When I bring up the page, it renders fine -- the drop-down list is correctly rendered. When I hit Submit, I get this strange error:

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]

I can't figure out what is null. It rendered fine, so it found the objects. What am I missing?


回答1:


Well, you're not showing the code for your SiteList class, but what's happening is that something -- almost certainly the dropdown -- doesn't have a model. So when wicket calls, essentially, dropdown.getModel().setModelObject( foo ) ; it gets a null pointer exception.

My suggestion is this, following the old OO rule of thumb to prefer composition to inheritance. Your SiteChoice and SiteList classes don't seem to add much, and they're making your errors harder to debug.

Instead, just add a DropDownChoice to your form:

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

That's more concise too,



来源:https://stackoverflow.com/questions/3452310/attempt-to-set-model-object-on-null-model-of-component

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!