javaapache-commons-beanutilsnosuchmethod

Remedy for java.lang.NoSuchMethodException: Unknown property 'Centres'


I am working on a project based on Struts2. I have a POJO called AddCentreNestedForm which has one of the property of type String[] called Centres.

So

public class AddCentreNestedForm{

....
....
private String[] Centres = new String[]{"-1","-1","-1","-1","-1","-1"};
...
...
public String[] getCentres(){
   return Centres;
}
public void setCentres(String[] Centres){
   this.Centres = Centres;
}

}

Now in the action class, i have a statement like below:

String[] centres =null;
...
...

centres = BeanUtils.getArrayProperty(addCentreNestedForm, "Centres");

on this line, it gives exception :

java.lang.NoSuchMethodException: Unknown property 'Centres'

'addCentreNestedForm' is an instance of POJO AddCentreNestedForm.

I debugged the application, when this line is executed, the value of Centres is {"Centres","-1"}.

On googling this exception, it mentions that this exception occurs when BeanUtils is not able to find proper getter method for the mentioned property.

Please let me know what should be the getter method name, I have named it as 'getCentres()'. Please let me know if there is any other solution to this problem.


Solution

  • I tired option of making 'Centres' property to 'centres', but after doing so, the value from jsp to POJO's property stopped being populated. I also made changes into jsp page and converted 'Centres' to 'centres' but of no help. Then I tried a simple solution and it worked. I simple used the getter method i.e. getCentres() and it worked. So I removed the line containing BeanUtils and instead, fetched the value from getCentres().