1

I have this DAO Class,

public class CountriesDAO {

    static final Logger logger = LogManager.getLogger(CountriesDAO.class.getName());

    public List<SelectCountriesBean> getAllCountries() {
        java.sql.Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<SelectCountriesBean> countries = new ArrayList<>();
        try {
            CreateConnection.loadProperties();
            System.out.println("Beginning database connection to retrieve countries list");
            conn = CreateConnection.getConnection();
            pstmt = conn.prepareStatement("select * from KIRAN.TBHHCOUNTRY");
            rs = pstmt.executeQuery();
            while (rs.next()) {
                SelectCountriesBean countrieslist = new SelectCountriesBean();
                countrieslist.setCNCOUNTRYCODE(rs.getString("CNCOUNTRYCODE"));
                countrieslist.setCNCOUNTRYNAME(rs.getString("CNCOUNTRYNAME"));
                countries.add(countrieslist);
            }
        } catch (SQLException ex) {
            logger.error("Countries retrival failed due to SQL Error", ex);
        }
        return countries;
    }
}

This is my bean,

 public class SelectCountriesBean {
        private String CNCOUNTRYCODE;
        private String CNCOUNTRYNAME;

        /**
         * @return the CNCOUNTRYCODE
         */
        public String getCNCOUNTRYCODE() {
            return CNCOUNTRYCODE;
        }

        /**
         * @param CNCOUNTRYCODE the CNCOUNTRYCODE to set
         */
        public void setCNCOUNTRYCODE(String CNCOUNTRYCODE) {
            this.CNCOUNTRYCODE = CNCOUNTRYCODE;
        }

        /**
         * @return the CNCOUNTRYNAME
         */
        public String getCNCOUNTRYNAME() {
            return CNCOUNTRYNAME;
        }

        /**
         * @param CNCOUNTRYNAME the CNCOUNTRYNAME to set
         */
        public void setCNCOUNTRYNAME(String CNCOUNTRYNAME) {
            this.CNCOUNTRYNAME = CNCOUNTRYNAME;
        }
    }

This is my action

public class getAllCountries extends ActionSupport implements Preparable {
     private List<SelectCountriesBean> countriesList;
     CountriesDAO allCountriesDAO = new CountriesDAO();

    public getAllCountries() {


    }
     @Override
    public void prepare(){

    }
    @Override
    public String execute() throws Exception {  
        countriesList = allCountriesDAO.getAllCountries();
       return "success";
    }

    /**
     * @return the countriesList
     */
    public List getCountriesList() {
        return countriesList;
    }

    /**
     * @param countriesList the countriesList to set
     */
    public void setCountriesList(List countriesList) {
        this.countriesList = countriesList;
    }


    }

This is my JSP

  <s:select name="COLCOUNTRY"  list="countriesList"  listKey="CNCOUNTRYCODE"  listValue="CNCOUNTRYNAME"  required="true"></s:select>

This is my struts.xml

<action name="getAllCountries" class="helpers.getAllCountries" method="execute" > 
            <result name="success">/WEB-INF/views/CreateAccountForm.jsp</result>
       </action> 

I get below message,

org.apache.jasper.JasperException: tag 'select', field 'list', name 'COLCOUNTRY': The requested list key 'countriesList' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name} - [unknown location]

I am out of thoughts now as why countriesList is not resolving as Collection.Can some one take a look and share some thoughts.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Kiran Badi
  • 501
  • 2
  • 9
  • 24

2 Answers2

0

Chances are the getCountriesList() method is returning null.

At runtime the system can't tell the type of a null so it returns that error.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
TheChrisPratt
  • 457
  • 3
  • 13
  • Let me add checks for null and see what it comes back with. – Kiran Badi Aug 07 '14 at 23:39
  • I think Chris action is not getting called at all for some reason.I don't see any logs being created.Actually this is form where I use struts tags.On page load, it has to trigger select action,my gut feeling is that is not getting called.Do we need any event to trigger s2 select tag ? – Kiran Badi Aug 07 '14 at 23:53
  • The error clearly says the countriesList is not found in the request. can you please temporarly add a countriesList Object in your request and see what is happening. You don't need any special event for select tag to work, but the values must be populated correctly. – Lyju I Edwinson Aug 08 '14 at 00:11
  • How do I add this in request. – Kiran Badi Aug 08 '14 at 00:29
  • I was correct, select tag needs some event to trigger action.I just pointed the selecttag action to test link using s:url tag and then clicked the link, I see all values are getting populated in drop down. Whereas no values are getting populated in my form when there is no event to trigger select action.I did no changes to above code. – Kiran Badi Aug 08 '14 at 01:41
  • Ok I resolved the issue and now select tag data is loading correctly.Thanks Chris and Lyju. Appreciate your assistance. – Kiran Badi Aug 08 '14 at 02:36
0
<s:select name="COLCOUNTRY"  list="countriesList"  listKey="CNCOUNTRYCODE"  listValue="CNCOUNTRYNAME"  required="true"></s:select>

MUST BE CHANGED TO

<s:select name="COLCOUNTRY"  list="allCountries"  listKey="CNCOUNTRYCODE"  listValue="CNCOUNTRYNAME"  required="true"></s:select>

As allCountries return the list of countries Not countriesList

Or is it true that you have an action class called class="helpers.getAllCountries" and it has a getCountriesList() method?

Also why your class name is getAllCountries ? I think it is not good to give such names to classes.

Lyju I Edwinson
  • 1,764
  • 20
  • 20
  • I have bunch of helper classes in helper folder.Yes I see your point in naming convention.I will correct this.Thanks – Kiran Badi Aug 07 '14 at 23:59