2010-08-10 1 views
4

Wicket을 처음 사용했지만이 문제를 검색해도 의미가 없었습니다. 그래서 나는 SO의 누군가가 도울 수 있기를 바라고있다.구성 요소의 null 모델에서 모델 객체를 설정하려고 시도했습니다.

Form을 확장하는 SiteChoice 개체와 DropDownChoice를 확장하는 SiteList 개체가 있습니다.

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

내 개찰구 템플릿이 있습니다 :

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

그럼 난 단지 라 내 Page 개체에 내 SiteChoice 개체를 추가 : 내 SiteChoice 클래스처럼 보이는

I 페이지를 표시하면 렌더링이 잘되며 드롭 다운 목록이 올바르게 렌더링됩니다. Submit을 누르면, 이상한 오류가 발생합니다 :

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이 무엇인지 알 수 없습니다. 그것은 잘 렌더링되었으므로 객체를 찾았습니다. 내가 뭘 놓치고 있니?

답변

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