1

I have defined an Employee object with the following properties

public class Employee {

    private String id;
    private String name;
    private Address address;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    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;
    }
}

The Address object:

public class Address {

    private String street;
    private String city;

    public String getStreet() {
        return street;
    }

    public void setStreet(String street) {
        this.street = street;
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }
}

And have generated the getters and setters for the properties. When I do the following on the properties:

String value = BeanUtils.getNestedProperty(employee, "address.street");

I get a "No Such Method Exception" for address.street.

java.lang.NoSuchMethodException: Unknown property 'address.street' on class 'class com.test.xm.Employee'

The fields id and name works fine.

Have double checked the getters and setters and it seems fine. What possibly could I be doing wrong here?

Edit: Have updated the getters and setters.

Wojciech Wirzbicki
  • 2,990
  • 4
  • 27
  • 47
user1583803
  • 401
  • 2
  • 5
  • 13

1 Answers1

4

Use PropertyUtils instead of BeanUtils.

   (String) PropertyUtilsBean.getInstance().getNestedProperty(employee, "address.street");  

For me your example is working well too. So in your example, the only possible error is: employee isn't an instance of Employee.

Also why don't you post the Exception message? They contain very helpful information, for eg:

 java.lang.NoSuchMethodException: Unknown property 'address' on class 'class com.mycompany.dto.Address'
user1583803
  • 401
  • 2
  • 5
  • 13
Ilya
  • 27,538
  • 18
  • 104
  • 148