Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

apache commons beanutils, how to set property value?

In the java, commons beanutils, try to set property 'address' and 'creditCardList' to object, but it gave me error :

java.lang.NoSuchMethodException: Property 'address' has no setter method in class 'class com.dao.Student'

but I have this method there. The code is here:

public class Main { 
    public static void main(String[] args) {
        Object student = new Student("John");       
        Object address = new Address("NJ");

        try {
            PropertyUtils.setProperty(student, "address", address);         
            //---------- 
            List list = new ArrayList();
            Object creditCard = new CreditCard();
            list.add(creditCard);

            PropertyUtils.setProperty(student, "creditCardList", list);         

        } catch (Exception e) {         
            e.printStackTrace();
        } 
    }
}

class Student {
    private String name;        
    private Address address;    
    private List<CreditCard> creditCardList;    
    public Student(String name) {
        super();
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public Address getAddress() {
        return address;
    }
    public void setAddress(Address address) {
        this.address = address;
    }
    public List<CreditCard> getCreditCardList() {
        return creditCardList;
    }
    public void setCreditCardList(List<CreditCard> creditCardList) {
        this.creditCardList = creditCardList;
    }   
}

class Address {
    private String name;
    public Address(String name) {
        super();
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }   
}

class CreditCard{
    private String cardName;

    public String getCardName() {
        return cardName;
    }

    public void setCardName(String cardName) {
        this.cardName = cardName;
    }   
}
like image 818
user595234 Avatar asked Dec 27 '22 10:12

user595234


2 Answers

Your class Student should be a public class , try making it public and rerun your code.

like image 134
mprabhat Avatar answered Jan 13 '23 06:01

mprabhat


I moved Student to a own file and made it public, that worked fine :)

like image 21
Andreas Wederbrand Avatar answered Jan 13 '23 06:01

Andreas Wederbrand