The plot thickens...
As suggested I put in a checker for null in getAsText of the CountryEditor and to return "" if that was the case.
I also put a debug point in my Country model in the equals() method.
The object being passed in is "", so I presume that the models being used to populate the list are null, even though the manager returns 3 valid objects to be populated in the drop down list. So somehow these objects (list of countries) are going from valid Country objects to null objects.

Aled Rhys Jones wrote:
Every time I try to use the below jsp tags I always get a jasper exception. Contained within I think is a NullPointerException.

Anything obviously wrong with the below please? I'm really struggling with this and this drop down issue is stalling me quite a bit.

All the beans are setup ok. I presume I don't need a config for the country editor since I'm not injecting anything but passing in the manager as a constructor. What is odd is that when debugging the set and get methods of the country editor are never called.

public class WebsiteFormController extends BaseFormController {

   private GenericManager<Website, Long> websiteManager = null;
   private GenericManager<Country, Long> countryManager = null;
public void setWebsiteManager(GenericManager<Website, Long> websiteManager) { this.websiteManager = websiteManager; } public void setCountryManager(GenericManager<Country, Long> countryManager) { this.countryManager = countryManager; }

public WebsiteFormController() { setCommandClass(Website.class); setCommandName("website"); } protected Object formBackingObject(HttpServletRequest request) throws Exception { String id = request.getParameter("id"); Website website;
       if (!StringUtils.isBlank(id)) {
           website = websiteManager.get(new Long(id));         }
       else{
           website = new Website();
           Url url = new Url();
           website.setUrl(url);
       }
       return website;
} public ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object command, BindException errors) throws Exception { log.debug("entering 'onSubmit' method..."); Website website = (Website) command; boolean isNew = (website.getId() == null); String success = getSuccessView(); Locale locale = request.getLocale(); if (request.getParameter("delete") != null) { websiteManager.remove(website.getId()); saveMessage(request, getText("websiteForm.deleted", locale)); } else { websiteManager.save(website); String key = (isNew) ? "websiteForm.added" : "websiteForm.updated"; saveMessage(request, getText(key, locale)); if (!isNew) { success = "redirect:websiteform.html?id=" + website.getId(); } } return new ModelAndView(success); } protected Map referenceData(HttpServletRequest request) throws Exception {
       Map retval = new HashMap<String, Object>();
       retval.put("countries", countryManager.getAll());
       return retval;
   }
     @Override
protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) { binder.registerCustomEditor(Country.class, "country", new CountryEditor(countryManager));
   }
}

public class CountryEditor extends PropertyEditorSupport {
     private GenericManager<Country, Long> countryManager = null;
     public CountryEditor(GenericManager<Country, Long> countryManager){
       this.countryManager = countryManager;
   }
     /**
   * (non-Javadoc)
   *
   * @see java.beans.PropertyEditor#setAsText(java.lang.String)
   */
   @Override
   public void setAsText(String id) throws IllegalArgumentException {
       Country country = countryManager.get(Long.valueOf(id));
       setValue(country);
   }
     @Override
   public String getAsText(){
       Country country = (Country) getValue();
       return String.valueOf(country.getId());
   }

}

<form:select path="contact.country">
   <form:option value="" label="label for no value"/>
   <form:options items="${countries}" itemLabel="name" itemValue="id" />
</form:select>

Any help would be appreciated.

Thanks
Aled

Michael Horwitz wrote:
If your properties can be null, then you need to make sure your PropertyEditor can handle null values properly - a bit of unit testing should provide the test cover you need. You also need to provide an option in your dropdown list to match to the null value. Something like: <form:select path="contact.country">
   <form:option value="" label="label for no value"/>
   <form:options items="${countries}" itemLabel="name" itemValue="id" />
</form:select>

Mike
On 7/8/07, *Aled Rhys Jones* <[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>> wrote:

    Further to this (still can't save drop down list changes :-( ),  I
    have
    an example where an object x contains an object y that can be
    selected
    using a drop down list, but y is currently null as I haven't added
    it to
    x yet (would be done using the drop down list).
    Is the trick here to create y in the formBackingObject and set the
    id to
    -1?  Currently I get a null pointer in the property editor,
    presumably
    as its trying to compare the id or name of a null object.

    Aled Rhys Jones wrote:
    > Thanks for the replies Mike.
    >
    > Two issues with the below is that it doesn't seem to like me using
    > spring:form.  Everything else seems to be form:form which seems
    to work.
    > I therefore tried the below using form:select, and I get the
    following
    > exception:
    > java.lang.NullPointerException
    >    at
    >
org.springframework.web.servlet.tags.form.SelectedValueComparator.exhaustiveCompare
    (SelectedValueComparator.java:154)
    >
    >    at
    >
org.springframework.web.servlet.tags.form.SelectedValueComparator.isSelected(SelectedValueComparator.java:90)
    >
    >    at
    >
    org.springframework.web.servlet.tags.form.OptionWriter.isSelected
    (OptionWriter.java:184)
    >
    >    at
    >
org.springframework.web.servlet.tags.form.OptionWriter.renderOption(OptionWriter.java:172)
    >
    >    at
    >
org.springframework.web.servlet.tags.form.OptionWriter.doRenderFromCollection
    (OptionWriter.java:155)
    >
    >
    > Cheers
    > Aled
    > Michael Horwitz wrote:
    >> Hmm. In that case you might want to take a look at Spring's
    form tags
    >> to make life a little easier:
    >>
http://static.springframework.org/spring/docs/2.0.x/reference/spring-form.tld.html
    >> <
http://static.springframework.org/spring/docs/2.0.x/reference/spring-form.tld.html>
    >>
    >>
    >> So your JSP would look something like:
    >>
    >> <spring:form commandName="website" ...>
    >>
    >> ...
    >>
    >> <spring:select path="contact.country"  items="${countries}"
    >> itemLabel="name" itemValue="id"/>
    >> ....
    >>
    >> </spring:form>
    >>
>> A lot simpler and Spring will do all the hard work for you. As long
    >> as your form backing object has a non-null contact attribute, it
    >> should all work like magic. I think the problem in your code is
    the
    >> name you attach to the select element - Spring expects specific
    names
    >> to get its bindings sorted out.
    >>
    >> Mike.
    >>
    >> On 7/6/07, *Aled Rhys Jones* < [EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>
    >> <mailto:[EMAIL PROTECTED] <mailto:[EMAIL PROTECTED]>>>
    wrote:
    >>
    >>     m4, wanted to finish off some functionality before updating
    to m5.
    >>
    >>
    >>     Cheers
    >>
    >>     Aled
    >>
    >>
    >>
    >>
------------------------------------------------------------------------
    >>
    >>     *From:* Michael Horwitz [mailto: [EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>
    >>     <mailto:[EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>>]
    >>     *Sent:* 06 July 2007 08:39
    >>     *To:* [email protected]
    <mailto:[email protected]>
    <mailto:[email protected]
    <mailto:[email protected]>>
    >>     *Subject:* Re: [appfuse-user] Drop down list population
    >>
    >>
    >>     Which version of AppFuse are you using Aled?
    >>
    >>     On 7/5/07, *Tsung* < [EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]> <mailto: [EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>>>
    >>     wrote:
    >>
    >>
    >>     Aled - Rather than having setCountryManager method, pass the
    >>     CountryManager
    >>     referene in CountryEditor's constructor.
    >>
    >>
    >>     --
    >>     View this message in context:
    >>
    >>
http://www.nabble.com/Add-ManyToOne-relationship-between-user-and-a-new-pojo-tf3993393s2369.html#a11454095
    >>
    >>
    >> <
http://www.nabble.com/Add-ManyToOne-relationship-between-user-and-a-new-pojo-tf3993393s2369.html#a11454095>
    >>
    >>     Sent from the AppFuse - User mailing list archive at
    Nabble.com <http://Nabble.com>
    >>     <http://nabble.com/>.
    >>
    >>
    >>
---------------------------------------------------------------------
    >>     To unsubscribe, e-mail:
    [EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>
    >>     <mailto:[EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]> >
    >>     For additional commands, e-mail:
    [EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>
    >>     <mailto:[EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>>
    >>
    >>
    >>
    >>
------------------------------------------------------------------------
    >>
    >> No virus found in this incoming message.
    >> Checked by AVG Free Edition. Version: 7.5.476 / Virus Database:
    >> 269.10.1/888 - Release Date: 06/07/2007 06:36
    >>
    >
    >
---------------------------------------------------------------------
    > To unsubscribe, e-mail: [EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>
    > For additional commands, e-mail: [EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>
    >
    >
    >
    >

---------------------------------------------------------------------
    To unsubscribe, e-mail: [EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>
    For additional commands, e-mail: [EMAIL PROTECTED]
    <mailto:[EMAIL PROTECTED]>


------------------------------------------------------------------------

No virus found in this incoming message.
Checked by AVG Free Edition. Version: 7.5.476 / Virus Database: 269.10.2/893 - Release Date: 09/07/2007 17:22

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]





---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to