Question

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;
    }   
}
Was it helpful?

Solution

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

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top