Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

wicket

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?

like image 653
Best Avatar asked Mar 02 '26 14:03

Best


2 Answers

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:

  • Using the DropDownChoice component
  • Working with Wicket Models
like image 108
Xavi López Avatar answered Mar 05 '26 02:03

Xavi López


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]

like image 27
Giovanni Avatar answered Mar 05 '26 03:03

Giovanni