commons-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Niall Pemberton" <niall.pember...@gmail.com>
Subject Re: Commons-beanutils indexdProperty error
Date Fri, 10 Oct 2008 20:20:05 GMT
On Fri, Oct 10, 2008 at 6:28 AM, Deep D <deeps4u@rocketmail.com> wrote:
> Hi,
>
> I am very new to the beanutils.
> I am trying to set value to an array list using indexed properties.
>
> It is throwing the following error
> Exception in thread "main" java.lang.
> IndexOutOfBoundsException: Index: 0, Size: 0
>    at java.util.ArrayList.rangeCheck(ArrayList.java:572)
>    at java.util.ArrayList.set(ArrayList.java:365)
>    at org.apache.commons.beanutils.PropertyUtilsBean.setIndexedProperty(PropertyUtilsBean.java:1711)
>
> Here is my  code

BeanUtils doesn't automatically grow arrays/Lists (except for
LazyDynaBean) - so while you can use the indexed setter to set an
indexed property the List needs to have elements that size already,
otherwise you see exactly the error you're getting. So you have a
number of ways you can resolve this:

1) Ensure that your List is always large enough - so if for example
you know that there will never been more than five addresses, then
initialize the list with five empty/null elements [not that I'm
recommending this - hacky]

2) Provide indexed getters/setters in your object (BeanUtils will use
if available these rather than the List getters/setters) and implement
"lazy list" growth. So in your address example something like:

https://java.sun.com/docs/books/tutorial/javabeans/properties/indexed.html

    public Address getAddr(int idx) {
        while (idx >= addr.size()) {
            addr.add(new Address()); //dynamically grow list to accomodate
        }
        return addr.get(idx);
    }

    public void setAddr(int idx, Address element) {
        while (idx >= addr.size()) {
            addr.add(new Address()); //dynamically grow list to accomodate
        }
        this.addr.set(idx, element);
    }

3) Use a lazy list to hold your addresses - for example:
http://commons.apache.org/collections/api-release/org/apache/commons/collections/list/LazyList.html

4) Use a LazyDynaBean, which supports lazy list/array growth out of the box
http://commons.apache.org/beanutils/v1.8.0/apidocs/org/apache/commons/beanutils/package-summary.html#dynamic.lazy

Niall

> import java.util.ArrayList;
> import java.util.List;
>
> public class Employee {
>
>    private String name;
>
>    private int id;
>
>    private String dept;
>
>    private List<Address> addr=new ArrayList<Address>();
>
>    public List<Address> getAddr() {
>        return addr;
>    }
>
>    public void setAddr(List<Address> addr) {
>        this.addr = addr;
>    }
>
>    public String getName() {
>        return name;
>    }
>
>    public void setName(String name) {
>        this.name = name;
>    }
>
>    public int getId() {
>        return id;
>    }
>
>    public void setId(int id) {
>        this.id = id;
>    }
>
>    public String getDept() {
>        return dept;
>    }
>
>    public void setDept(String dept) {
>        this.dept = dept;
>    }
>
> }
>
>
>
> public class Address {
>
>
>
>
>    private String line;
>
>    private String aptno;
>
>    public String getLine() {
>        return line;
>    }
>
>    public void setLine(String line) {
>        this.line = line;
>    }
>
>    public String getAptno() {
>        return aptno;
>    }
>
>    public void setAptno(String aptno) {
>        this.aptno = aptno;
>    }
>
>
> }
>
>
> public void indexdPropTest(){
>        Employee emp=new Employee();
>        Address addr=new Address();
>        try {
>            PropertyUtils.setSimpleProperty(addr, "line","36777" );
>            PropertyUtils.setIndexedProperty(emp,"addr",0,addr);
>        } catch (IllegalAccessException e) {
>            // TODO Auto-generated catch block
>            e.printStackTrace();
>        } catch (InvocationTargetException e) {
>            // TODO Auto-generated catch block
>            e.printStackTrace();
>        } catch (NoSuchMethodException e) {
>            // TODO Auto-generated catch block
>            e.printStackTrace();
>        }
>
>
>    }
>
> Can somebody advise me if anything wrong with this? I had tried replacing arraylist with
an array and it was successful.
>
> Thanks,
> D
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@commons.apache.org
For additional commands, e-mail: user-help@commons.apache.org


Mime
View raw message