尝试在组件的空模型上设置模型对象 [英] Attempt to set model object on null model of component

查看:28
本文介绍了尝试在组件的空模型上设置模型对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 Wicket 的新手,但是在谷歌上搜索这个问题并没有给我任何有意义的信息.所以我希望 SO 中的某个人可以提供帮助.

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.

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

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

然后我只需将我的 SiteChoice 对象添加到我的 Page 对象中:

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

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

我的 Wicket 模板有:

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?

推荐答案

好吧,您没有显示 SiteList 类的代码,但是发生的事情是某些东西——几乎可以肯定是下拉列表——没有模型.因此,当 wicket 调用时,本质上是 dropdown.getModel().setModelObject( foo ) ; 它会得到一个空指针异常.

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.

我的建议是这样的,遵循旧的 OO 经验法则,更喜欢组合而不是继承.您的 SiteChoiceSiteList 类似乎没有增加太多,而且它们使您的错误更难调试.

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.

相反,只需在表单中添加一个 DropDownChoice:

Instead, just add a DropDownChoice to your form:

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

这样也更简洁,

这篇关于尝试在组件的空模型上设置模型对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆