Hi Reinout,

I guess your Person object has a private int sex; instead of private SexType sex; So what happens is that you are trying to convert a SexType to an Integer which obviously returns an
illegalArgumentException.

Put it like this and it should work.

public class Person {
    private SexType sex;

    public enum SexType {
SEX_UNKNOWN(0), SEX_MALE(1), SEX_FEMALE(2), SEX_NOT_APPLICABLE(9);
        private int id;

        SexType(int id) { this.id = id; }

        public int getId() { return this.id; }
        public String getName() { return this.name(); }
    }


    public Person() {}

    public SexType getSex() { return sex; }
}

and the Page as follows:

public class TestPage extends WebPage {

    public TestPage() {
Form<Person> form = new Form<Person>("form", new CompoundPropertyModel<Person>(new Person())); form.add(new RadioChoice<SexType>("sex", Arrays.asList(SexType.values()), new ChoiceRenderer<SexType>() {
            private static final long serialVersionUID = 1L;

            @Override
            public String getDisplayValue(SexType sexType) {
                return getString(sexType.toString());
            }
        }));
        add(form);
    }
}

Kind regards,

Mischa Dasberg

Op 14 aug 2009, om 18:43 heeft Reinout van Schouwen het volgende geschreven:

Hi all,

If I have an enum 'SexTypes' in the class Person like this:

====
 public static enum SexTypes {
   SEX_UNKNOWN(0),
   SEX_MALE(1),
   SEX_FEMALE(2),
   SEX_NOT_APPLICABLE(9);
/* Values for sex are taken from ISO 5218:1977 Representation of Human Sexes */
   private final int intValue;

   SexTypes(int intValue) {
     this.intValue = intValue;
   }

   public int getIntValue() {
     return this.intValue;
   }

   public String getName() {
     return this.name();
   }
 }
====

...And I want to display them, localized, in a Form like this:

====
ChoiceRenderer renderer = new ChoiceRenderer("name", "intValue")
     {
       @Override
       public String getDisplayValue(Object object) {
         return getString((String)super.getDisplayValue(object));
       }
     };
form.add(new RadioChoice("sex", Arrays.asList(Person.SexTypes.values()), renderer));
   }
====

...then, when the form is submitted, I get an IllegalArgumentException:

Caused by: java.lang.IllegalArgumentException: Cannot format given Object as a Number
       at java.text.DecimalFormat.format(DecimalFormat.java:504)
       at java.text.Format.format(Format.java:157)
at org .apache .wicket .util .convert .converters .AbstractNumberConverter .convertToString(AbstractNumberConverter.java:109) at org .apache .wicket .util .lang .PropertyResolverConverter.convert(PropertyResolverConverter.java:84) at org.apache.wicket.util.lang.PropertyResolver $MethodGetAndSet.setValue(PropertyResolver.java:1094) at org.apache.wicket.util.lang.PropertyResolver $ObjectAndGetSetter.setValue(PropertyResolver.java:589) at org .apache .wicket.util.lang.PropertyResolver.setValue(PropertyResolver.java:137) at org .apache .wicket .model.AbstractPropertyModel.setObject(AbstractPropertyModel.java:164) at org.apache.wicket.Component.setModelObject(Component.java: 2934) at org .apache .wicket .markup.html.form.FormComponent.updateModel(FormComponent.java:1069) at org.apache.wicket.markup.html.form.Form $21.validate(Form.java:1866) at org.apache.wicket.markup.html.form.Form $ValidationVisitor.formComponent(Form.java:166) at org .apache .wicket .markup .html .form .FormComponent.visitFormComponentsPostOrderHelper(FormComponent.java: 421) at org .apache .wicket .markup .html .form .FormComponent.visitFormComponentsPostOrderHelper(FormComponent.java: 408) at org .apache .wicket .markup .html .form.FormComponent.visitFormComponentsPostOrder(FormComponent.java: 385) at org .apache .wicket.markup.html.form.Form.visitFormComponentsPostOrder(Form.java: 1089) at org .apache .wicket .markup.html.form.Form.internalUpdateFormComponentModels(Form.java: 1858) at org .apache .wicket.markup.html.form.Form.updateFormComponentModels(Form.java: 1825) at org.apache.wicket.markup.html.form.Form.process(Form.java: 871) at org.apache.wicket.markup.html.form.Form.onFormSubmitted(Form.java:808)
       ... 30 moremberFormatException:
====

I believe this is caused because the DecimalFormat.format() method is
being passed a SexType instead of a Number object. My question is, how
can I solve this elegantly so that the radiochoice value will be
resolved to a number?

regards,

--
Reinout van Schouwen


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@wicket.apache.org
For additional commands, e-mail: users-h...@wicket.apache.org


Reply via email to