2010-08-10 68 views
4

我是新來的Wicket,但是使用Google搜索這個問題並沒有給我任何有意義的東西。所以我希望有人可以幫助。嘗試在組件零模型上設置模型對象

我有一個SiteChoice對象,它擴展了Form,還有一個SiteList對象,它可以擴展爲 DropDownChoice。我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對象添加到我的網頁對象一拉:

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

我的檢票模板有:

當我調出頁面,呈現良好 - 正確呈現下拉列表。當我點擊提交時,我得到這個奇怪的錯誤:

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] 

我找不出什麼是空的。它呈現良好,因此它找到了對象。我錯過了什麼?

回答

12

嗯,你沒有顯示你的SiteList類的代碼,但是發生的事情是 - 某些事情 - 幾乎可以肯定是下拉菜單 - 沒有模型。所以當Wicket調用時,本質上是dropdown.getModel().setModelObject(foo) ;它得到一個空指針異常。

我的建議是這樣的,遵循舊的OO經驗法則寧願組合繼承。你的SiteChoiceSiteList類看起來並不多,而且它們讓你的錯誤更難調試。

相反,只需添加一個DropDownChoice到您的窗體:

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

這是更簡潔也

+0

感謝tpdi。這工作。 – MikeHoss 2010-08-10 19:18:44