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

发布于 2024-09-13 08:56:15 字数 1451 浏览 7 评论 0原文

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

我有一个扩展 Form 的 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 对象添加到我的 Page 对象即可:

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

我的 Wicket 模板具有:

当我打开该页面时,它呈现良好 - 下拉列表已正确呈现。当我点击“提交”时,我收到这个奇怪的错误:

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]

我无法弄清楚什么是 null。它渲染得很好,所以它找到了对象。我缺少什么?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

轻拂→两袖风尘 2024-09-20 08:56:15

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

我的建议是,遵循旧的面向对象的经验法则,优先选择组合而不是继承。您的 SiteChoiceSiteList 类似乎没有添加太多内容,而且它们使您的错误更难以调试。

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

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

这也更简洁,

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,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文