尝试在组件的空模型上设置模型对象
我是 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,您没有显示 SiteList 类的代码,但发生的情况是某些东西(几乎可以肯定是下拉列表)没有模型。因此,当 wicket 本质上调用
dropdown.getModel().setModelObject( foo ) ;
时,它会得到一个空指针异常。我的建议是,遵循旧的面向对象的经验法则,优先选择组合而不是继承。您的
SiteChoice
和SiteList
类似乎没有添加太多内容,而且它们使您的错误更难以调试。相反,只需在表单中添加 DropDownChoice:
这也更简洁,
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
andSiteList
classes don't seem to add much, and they're making your errors harder to debug.Instead, just add a DropDownChoice to your form:
That's more concise too,