PropertyUtils.getProperty error while trying to get a simple property value

I have a strange problem with the method PropertyUtils.getProperty(bean, fieldName)where I got it java.lang.NoShuchMethodException.

Suppose we have a simple java class called pojo:

public class Pojo {
    public java.util.Date aDate;
    public java.util.Date theDate;

    public Pojo(){}
}

and caller class for example

public class TestPojo{
    public static void main(String[] args){
        Pojo p = new Pojo();
        p.setADate(new Date());
        p.setTheDate(new Date());

        PropertyUtils.getProperty(p, "theDate");
        PropertyUtils.getProperty(p, "aDate");
    }
}

The first call PropertyUtils.getPropertyworks fine, and the second - throw NoSuchMethodExeption.

I would like to know if I am missing something stupid or is it really a mistake :)

+5
source share
4 answers

I do not understand how it PropertyUtils.getProperty(p, "TheDate");can work, since the property name is incorrect.

Try the following:

public class TestPojo{
    public static void main(String[] args){
        Pojo p = new Pojo();
        p.setADate(new Date());
        p.setTheDate(new Date());

        PropertyUtils.getProperty(p, "theDate");
        PropertyUtils.getProperty(p, "aDate");
    }
}

PropertyUtils Link

To solve your problem, two solutions:

  • "ADate"
  • getaDate() setaDate (Date dateToSet)

,

+4

Java Bean "8.8. ", , , " ".

( ):

, getter "getADate" , "ADate" , "aDate" .

, , :

  • "ADate"
  • "getaDate" "setaDate"
+6

You might need:

PropertyUtils.getProperty(p, "ADate");

where A is in UPPERCASE

+2
source

Try

PropertyUtils.getProperty(p, "ADate");

instead

PropertyUtils.getProperty(p, "ADate");
+1
source

All Articles