Best
Best

Reputation: 2318

PropertyModel Expression's Exception : org.apache.wicket.WicketRuntimeException: No get method defined for class:

I used PropertyModel as the part of my DropDownChoice as following:

    List<String> choices = Arrays.asList(new String[] { "Library", "School Office", "Science Dept" });
    String selected = "Library";

    DropDownChoice<String> serviceDDC = 
            new DropDownChoice<String>("service",  new PropertyModel(this, "choices.0"), choices); 

Somehow I've got this exception thown:

caused by: org.apache.wicket.WicketRuntimeException: No get method defined for class: class com.samoo.tool.pages.CreatePrintingJob expression: choices
    at org.apache.wicket.util.lang.PropertyResolver.getGetAndSetter(PropertyResolver.java:481)
    at org.apache.wicket.util.lang.PropertyResolver.getObjectAndGetSetter(PropertyResolver.java:332)
    at org.apache.wicket.util.lang.PropertyResolver.getObjectAndGetSetter(PropertyResolver.java:242)
    at org.apache.wicket.util.lang.PropertyResolver.getValue(PropertyResolver.java:95)
    at org.apache.wicket.model.AbstractPropertyModel.getObject(AbstractPropertyModel.java:130)
    at org.apache.wicket.Component.getDefaultModelObject(Component.java:1724)

....

I know that there's something wrong with the expression. I've been trying different parameter inputs but it still doesn't work. Could anyone help?

Upvotes: 3

Views: 13163

Answers (3)

Xavi L&#243;pez
Xavi L&#243;pez

Reputation: 27880

Since you're using PropertyModel(this, "choices.0"), Wicket is trying to find a property named choices via reflection through a method getChoices() of the class declaring the PropertyModel. This method doesn't seem to exist in com.samoo.tool.pages.CreatePrintingJob, as the exception is stating.

Also, if that 0 is an index, you should be accessing it with the [index] expression, as this JIRA issue suggests: PropertyModel does not support index only property ("[0]")

However, it seems you want to initialize the DropDownChoice to the first element of choices. But What Wicket will do if you set the DropDownChoice's Model to PropertyModel(this, "choices.[0"]) will be mapping the selection of this DropDownChoice in the following way:

  • At form rendering time to present the (pre)selected choice, it will use the first element in the choices list.
  • At form submission time to store the user selected value, it will store the selection in the first position of the choices list.

Summarising, the backing object representing the DropDownChoice's selection would be the first element in the choices list.

So, you'll probably want to use a whole different Model, independent from the choices list, for the backing object representing the DDC's selection.

List<String> choices = Arrays.asList(new String[] { "Library", "School Office", 
       "Science Dept" });
String selected = "Library";
IModel dropdownModel = new Model<String>(choices[0]);
DropDownChoice<String> serviceDDC = 
        new DropDownChoice<String>("service",  dropdownModel, choices);

You might find the following links useful:

Upvotes: 7

Javid Dadashkarimi
Javid Dadashkarimi

Reputation: 89

It is good idea to use IModel instead of PropertyMOdel.PropertyModel has big problems in refactoring. In my cases I did it and the problems solved properly.Also I have override the toString() of my Topic object.

topicDropDown = new DropDownChoice<Topic>("topicOptions", new IModel<Topic>() {
        @Override
        public Topic getObject() {
            return top;
        }

        @Override
        public void setObject(Topic t) {
            top = t;
        }

        @Override
        public void detach() {
        }
    }, new LoadableDetachableModel<List<Topic>>() {
        @Override
        protected List<Topic> load() {

            List<Topic> topics = top.getAllTopics();
            return topics;

        }
    });

Upvotes: 0

Giovanni
Giovanni

Reputation: 4015

you are declaring choices inside the method, in order to get the PropertyModel to work you need to declare it on a class level not on a method level. As @Xavi López pointed out the espression is not corret you nedd to use choices.[0]

Upvotes: 1

Related Questions